-
Notifications
You must be signed in to change notification settings - Fork 1
/
143. Reorder List
49 lines (48 loc) · 1.07 KB
/
143. Reorder List
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution
{
public void reorderList(ListNode head)
{
ListNode t1=head;
ListNode t2=head;
int n=0;
t2=t2.next;
while(t2!=null && t2.next!=null)
{
t1=t1.next;
t2=t2.next.next;
}
t2=t1;
t1=t1.next;
t2.next=null;
t2=head;
ListNode prev = null;
ListNode current = t1;
ListNode next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
t1 = prev;
while(t1!=null)
{
prev=t2;
t2=t2.next;
prev.next=t1;
t1=t1.next;
prev=prev.next;
prev.next=t2;
}
}
}