-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_linkedlist.java
More file actions
147 lines (140 loc) · 3.73 KB
/
reverse_linkedlist.java
File metadata and controls
147 lines (140 loc) · 3.73 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null||m==n) return head;
int i=1;
ListNode d=new ListNode(1);d.next=head;
ListNode pre=d,start=head,end,p;
while(i++!=m) {pre=start;start=start.next;}
end=start.next; p=start;
while(i++<=n) {
ListNode next=end.next;
end.next=p;
p=end;
end=next;
}
pre.next.next=end;
pre.next=p;
return d.next;
}
}
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
// Start typing your Java solution below
// DO NOT write main() function
int i=1;
ListNode d=new ListNode(1);
d.next=head;
ListNode pre=d,cur=head;
// find the head
while(i!=m&&cur!=null) {
pre=cur;
cur=cur.next;
i++;
}
int j=i;
ListNode next=cur;
// find the tail
while(j!=n&&next!=null) {
next=next.next;
j++;
}
ListNode p=next.next;
next.next=null; // make the tail null then call reverse
reverse(cur,p);
pre.next=next;
return d.next;
}
public void reverse(ListNode node,ListNode pre) {
if(node==null) return;
reverse(node.next,node);
node.next=pre;
}
}
ListNode previous=null;
public ListNode reverse(ListNode p) {
if(p==null) return null;
ListNode next=p.next;
if(previous==null)
p.next=null;
else
p.next=previous;
previous=p;
reverse(next);
return p;
}
previous will be the new head
http://leetcode.com/2010/04/reversing-linked-list-iteratively-and.html
public class Solution {
ListNode h;
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null) return null;
ListNode d=new ListNode(0);
d.next=head;
ListNode pre=d,s=head,e=head,next;
int i=1;
while(true) {
while(e!=null&&i<k) {
i++;
e=e.next;
}
if(e==null) return d.next;
next=e.next;
e.next=null;
reverse(s,next);
pre.next=e;
pre=s;
s=s.next;
e=s;
i=1;
}
//return d.next;
}
public void reverse(ListNode node,ListNode pre) {
if(node==null) return;
reverse(node.next,node);
node.next=pre;
}
}
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(m==n) return head;
ListNode previous=null,curr=head,next=curr.next,left=null;
int i=1;
while(i!=m) { //i++!=m
previous=curr;
curr=next;
curr=curr.next;
i++;
}
left=previous;
next=curr.next;
previous=curr;
curr=next;
next=next.next;
i++;
while(i!=n+1) {
curr.next=previous;
previous=curr;
curr=next;
if(next!=null)
next=next.next;
i++;
}
if(left==null) {
head.next=curr;
return previous;
}
else {
left.next.next=curr;
left.next=previous;
return head;
}
}
}
http://discuss.leetcode.com/questions/206/reverse-nodes-in-k-group