【LeetCode】206. 反转链表

leetcode链接 206. 反转链表
【LeetCode】206. 反转链表_第1张图片

#include 

struct ListNode {
    int val;
    struct ListNode* next;
};
typedef struct ListNode ListNode;

struct ListNode* reverseList1(struct ListNode* head) {
    if (head != NULL) {
        ListNode* n1 = NULL;
        ListNode* n2 = head;
        ListNode* n3 = n2->next;
        while (n2 != NULL) {
            n2->next = n1;
            n1 = n2;
            n2 = n3;
            if (n3 != NULL) {
                n3 = n3->next;
            }
        }
        return n1;
    }
    return NULL;
}

ListNode* reverseList2(ListNode* head) {
    if (head) {
        ListNode* newhead = NULL;
        ListNode* cur = head;
        while (cur) {
            ListNode* next = cur->next;
            cur->next = newhead;
            newhead = cur;
            cur = next;
        }
        return newhead;
    }
    return NULL;
}

你可能感兴趣的:(Data,Structure,and,Algorithm,刷题,C语言,leetcode,数据结构,算法,c语言,链表)