forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(Leetcode)Reverse Linked List.cpp
48 lines (45 loc) · 1.05 KB
/
(Leetcode)Reverse Linked List.cpp
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverse(ListNode *head){
ListNode *cur=head,*temp=NULL, *x;
while(cur)
{
x=cur->next;
cur->next=temp;
temp=cur;
cur=x;
}
return temp;
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode *t1=head,*t2=head,*start=t1,*end=t1;
int a=m, b=n;
if(m==n)return head;
while(m-->1){
start=t1;
t1=t1->next;
}
while(n>1){
t2=t2->next;
n--;
}
end=t2->next;
t2->next=NULL;
if(a==1){
head=reverse(head);
}
else start->next=reverse(t1) ;
t1->next=end;
return head;
}
};