-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse Linked List.cpp
More file actions
35 lines (32 loc) · 871 Bytes
/
Copy pathReverse Linked List.cpp
File metadata and controls
35 lines (32 loc) · 871 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
#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* reverseList(ListNode* head) {
if (head==NULL)
return head;
if (head->next==NULL)
return head;
ListNode* tmp=reverseList(head->next);
ListNode* tmpt=tmp;
while (tmpt->next!=NULL)
{
tmpt=tmpt->next;
}
tmpt->next=head;
head->next=NULL;
return tmp;
}
int main()
{
ListNode* head=new ListNode(1);
head->next=new ListNode(2);
head->next->next=new ListNode(3);
head->next->next->next=new ListNode(4);
ListNode* h=reverseList(head);
cout<<h->val<<endl<<h->next->val<<endl<<h->next->next->val<<endl<<h->next->next->next->val;
return 0;
}