-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseLinkedListII.cpp
More file actions
55 lines (52 loc) · 1.09 KB
/
ReverseLinkedListII.cpp
File metadata and controls
55 lines (52 loc) · 1.09 KB
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
/*
Question:
Reverse Link List II
Asked in:
Facebook
Microsoft
Amazon
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* Solution::reverseBetween(ListNode* A, int B, int C) {
struct ListNode *s=A,*e=A,*ps=A,*pe=A;
if(A->next==NULL)
return A;
for(int i=1;i<B;i++){
ps=s;
s=s->next;
}
for(int i=1;i<C;i++){
pe=e;
e=e->next;
}
if(e)
pe=e->next;
else
pe=e;
//cout<<s->val<<" "<<e->val<<" "<<ps->val<<" "<<pe->val<<endl;
struct ListNode *cur=s,*prev=e->next,*next=s->next;
while(cur!=pe)
{
//cout<<prev->val<<" "<<cur->val<<" "<<cur->next->val<<endl;
next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
}
if(s!=A)
ps->next=prev;
else
A=prev;
//prev->next=ps;
return A;
}