-
Notifications
You must be signed in to change notification settings - Fork 0
/
143.java
37 lines (37 loc) · 925 Bytes
/
143.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if(head==null) return;
ListNode r = head;
ListNode l = head;
while(r.next!=null&&r.next.next!=null){
l = l.next;
r = r.next.next;
}
ListNode head2 = new ListNode(0);
ListNode p = l.next;
l.next = null;
while(p!=null){
ListNode tmp = p.next;
p.next = head2.next;
head2.next = p;
p = tmp;
}
head2 = head2.next;
ListNode head1 = head;
while(head2!=null){
ListNode temp = head1.next;
head1.next = head2;
head2 = head2.next;
head1.next.next = temp;
head1 = temp;
}
}
}