-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReverseLinkedList.java
More file actions
133 lines (117 loc) · 3.1 KB
/
ReverseLinkedList.java
File metadata and controls
133 lines (117 loc) · 3.1 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
package linkedList;
/**
* Reverse a linked list in-place and in one pass.
*
* For linked list 1->2->3, the reversed linked list is 3->2->1.
*
* Variation: reverse a portion only.
*
* @author ruifengm
* @since 2018-Jun-17
*
* https://www.lintcode.com/problem/reverse-linked-list/description
* https://www.lintcode.com/problem/reverse-linked-list-ii/description
*/
public class ReverseLinkedList {
private static class ListNode {
int key;
ListNode next;
public ListNode(int key) {
this.key = key;
this.next = null;
}
}
/**
* Reverse the linked list recursively.
*/
public static ListNode newHead;
private static ListNode recursiveReverse(ListNode node) {
if (node.next == null) {
newHead = node;
return node;
}
ListNode tail = recursiveReverse(node.next);
tail.next = node; // reverse
node.next = null;
return node;
}
/**
* Reverse the linked list iteratively.
*/
private static ListNode iterativeReverse(ListNode head) {
ListNode prev = null, next;
while(head != null) {
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
/**
* Reverse the linked list from position m to n iteratively.
*/
private static ListNode iterativePartialReverse(ListNode head, int m, int n) {
if (head == null) return null;
int pos = 1;
ListNode pre = head, cur = head.next, temp = null, subTail = null, subHead = null;
// look for entry position
while (pos < m) {
subHead = pre; // used to connect the portion ahead of reversal
pre = pre.next;
cur = pre.next;
pos++;
}
subTail = pre; // used to connect to the portion behind reversal
// perform reverse
while (pos < n && cur != null) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
pos++;
}
subTail.next = cur;
if (subHead == null) {
head.next = cur;
head = pre;
} else subHead.next = pre;
return head;
}
private static void print(ListNode head) {
if (head == null) return;
while (true) {
System.out.print(head.key);
head = head.next;
if (head == null) break;
System.out.print("->");
}
System.out.println();
}
public static void main(String[] args) {
System.out.println("Welcome to the rabbit hole of reversed singly linked list.");
/* Full reversal */
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
print(head);
// recursiveReverse(head);
// print(newHead);
ListNode newHead = iterativeReverse(head);
print(newHead);
/* Partial reversal */
ListNode head2 = new ListNode(1);
head2.next = new ListNode(2);
head2.next.next = new ListNode(3);
head2.next.next.next = new ListNode(4);
head2.next.next.next.next = new ListNode(5);
head2.next.next.next.next.next = new ListNode(6);
print(head2);
ListNode newHead2 = iterativePartialReverse(head2, 2, 4);
// ListNode newHead2 = iterativePartialReverse(head2, 1, 6);
// ListNode newHead2 = iterativePartialReverse(head2, 1, 5);
// ListNode newHead2 = iterativePartialReverse(head2, 2, 6);
print(newHead2);
System.out.println("All rabbits gone.");
}
}