-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseLinkedList.java
More file actions
52 lines (40 loc) · 1.02 KB
/
reverseLinkedList.java
File metadata and controls
52 lines (40 loc) · 1.02 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
/*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.*/
puublic class Solution{
public void reverse(ListNode pre, ListNode endNext){
ListNode cur=pre.next;
while (cur.next!=endNext){
ListNode next=cur.next;
ListNode temp=pre.next;
pre.next=next;
cur.next=next.next;
next.next=temp;
}
public ListNode reverseList(ListNode head, int m, int n){
if (head == null || head.next == null){
return head;
}
ListNode temp = new ListNode(0);
temp.next=head;
ListNode pre = temp;
ListNode cur = head;
ListNode last = head;
int retN = n;
int retM = m;
while (retM < m || retN < n){
if(retM < m){
pre = pre.next;
cur = cur.next;
retM++;
}
if(retN < n){
end = end.next;
retN++;
}
}
reverse(pre,end.next);
return temp.next;
}
}