blob: 89358f85ff7b7097eb14ca04add8158fc0511ea7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// 206. Reverse Linked List
#include "leetcode.h"
/*
* given the 'head' of a singly linked list, reverse the list, and return the
* reversed list.
*/
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *reverseList(struct ListNode *head) {
if (!head || !head->next)
return head;
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = head->val;
new->next = NULL;
head = head->next;
while (head) {
struct ListNode *tmp = new;
new = head;
head = head->next;
new->next = tmp;
}
return new;
}
|