forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
70 lines (61 loc) · 1.73 KB
/
Solution.java
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
public class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head, fast = head.next.next;
while (fast != null) {
if (fast.next == null) break;
fast = fast.next.next;
slow = slow.next;
}
ListNode curr2 = reverse(slow.next);
slow.next = null;
ListNode curr = head, curr1 = head.next;
while (curr1 != null || curr2 != null) {
if (curr2 != null) {
curr.next = curr2;
curr2 = curr2.next;
curr = curr.next;
}
if (curr1 != null) {
curr.next = curr1;
curr1 = curr1.next;
curr = curr.next;
}
}
}
public ListNode reverse(ListNode head) {
ListNode swap = null, oldList = head, newList = null;
while (oldList != null) {
swap = oldList;
oldList = oldList.next;
swap.next = newList;
newList = swap;
}
return newList;
}
public void printList(ListNode e) {
while (e != null) {
System.out.format("\t%d", e.val);
e = e.next;
}
System.out.format("\n");
}
public static void main(String[] args) {
ListNode a = new ListNode(1);
a.next = new ListNode(2);
a.next.next = new ListNode(3);
a.next.next.next = new ListNode(4);
a.next.next.next.next = new ListNode(5);
Solution s = new Solution();
s.reorderList(a);
s.printList(a);
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}