-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143. Reorder List
78 lines (69 loc) · 1.87 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
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
class Solution {
public ListNode mid(ListNode head){
int size=0;
ListNode temp = head;
while(temp != null){
temp=temp.next;
size++;
}
// System.out.println(size);
// return head;
int n=size%2==0?size:size+1;
temp = head;
for(int i=0;temp != null && i<n/2-1;i++){
temp=temp.next;
}
// System.out.println(temp.val);
return temp;
}
public void rev(ListNode mid){
ListNode temp = mid;
ListNode tail = mid.next;
ListNode prev = mid;
ListNode curr = mid.next;
if(curr.next == null){
return ;
}
ListNode next = curr.next;
while(curr != null){
System.out.println("op");
curr.next=prev;
prev = curr;
curr = next;
if(next != null){
next = next.next;
System.out.println("po");
}
}
temp.next = prev;
tail.next = null;
}
public void reorderList(ListNode head) {
if(head == null || head .next == null) return ;
ListNode mid = mid(head);
rev(mid);
// ListNode temp = head;
// while(temp != null){
// System.out.println(temp.val);
// temp = temp.next;
// }
ListNode h1 = head;
ListNode h2 = mid.next;
ListNode check = mid.next;
while(h1 != null && h2 != null){
ListNode temp = h1.next;
h1.next = h2;
h1 = temp;
if(h1 == check){
return ;
// System.out.println("hhi");
}
temp = h2.next;
h2.next = h1;
h2=temp;
}
if(h1 != null){
h1.next = null;
}
}
}