【LeetCode】206. 反转链表
leetcode链接 206. 反转链表
#include <stdio.h>
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;
}