0
点赞
收藏
分享

微信扫一扫

LeetCode刷题笔记-剑指 Offer II 024. 反转链表

LeetCode刷题笔记-剑指 Offer II 024. 反转链表

C语言

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head){
    struct ListNode 
        *pre = NULL,
        *cur = NULL,
        *tail = NULL,
        *next = NULL;

    cur = head;
    while (cur != NULL) {
        tail = cur->next;
        if (tail == NULL) {
            cur->next = pre;
            pre = cur;
            break;
        }
        next = tail->next;
        cur->next = pre;
        tail->next = cur;
        pre = tail;
        cur = next;
    }
    return pre;
}

注意点

  1. cur->next == NULLtail为空时,应该注意curpre节点的链接避免遗漏最后这个cur节点;

结果

在这里插入图片描述

题目

在这里插入图片描述

举报

相关推荐

0 条评论