-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReorderList.java
45 lines (40 loc) · 945 Bytes
/
ReorderList.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
package leetcode;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class ReorderList {
public static void reorderList(ListNode head) {
ListNode cur = head;
ListNode tmp;
while(head!=null && head.next!=null){
while(cur.next.next!=null){
cur=cur.next;
}
tmp = cur.next;
cur.next=null;
tmp.next = head.next;
head.next = tmp;
head = tmp.next;
cur = head;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode node = new ListNode(1);
node.next = new ListNode(2);
node.next.next = new ListNode(3);
node.next.next.next = new ListNode(4);
node.next.next.next.next = new ListNode(5);
reorderList(node);
ListNode cur = node;
while(cur!=null){
System.out.println(cur.val);
cur=cur.next;
}
}
}