-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseLinkedList.cpp
More file actions
executable file
·56 lines (47 loc) · 974 Bytes
/
ReverseLinkedList.cpp
File metadata and controls
executable file
·56 lines (47 loc) · 974 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// ReverseLinkedList.cpp
// leetcode
//
// Created by witwolf on 5/5/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <stdio.h>
/**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head) {
if(!head || !head->next){
return head;
}
struct ListNode* prev = NULL,*cur = head,*next = cur->next;
while(cur && next){
cur->next = prev;
prev = cur;
cur = next ;
next = cur->next;
}
cur->next = prev;
return cur;
}
void print(struct ListNode *head){
while(head){
printf("%d->",head->val);
head = head->next;
}
printf("NULL\n");
}
int main(int argc,char **argv){
struct ListNode n1,n2,n3;
n1.val = 1;
n1.next = &n2;
n2.val = 2;
n2.next = &n3;
n3.val = 3;
n3.next = NULL;
print(&n1);
print(reverseList(&n1));
}