-
Notifications
You must be signed in to change notification settings - Fork 0
/
143. Reorder List.java
57 lines (57 loc) · 1.76 KB
/
143. Reorder List.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
{
if(count<=mid)
{
if(firstList==null)
{
firstList=insertNode(firstList,temp.val);
rareFirst=firstList;
}
else
{
rareFirst=insertNode(rareFirst,temp.val);
}
}
else
{
if(secondList==null)
{
secondList=new ListNode(temp.val);
}
else
{
ListNode ptr=new ListNode(temp.val);
ptr.next=secondList;
secondList=ptr;
}
}
count++;
temp=temp.next;
}
count=1;
ListNode root=firstList;
ListNode prev=null;
while(root!=null)
{
if(count%2==1 && secondList!=null)
{
ListNode temp1=root.next;
root.next=new ListNode(secondList.val);
root.next.next=temp1;
secondList=secondList.next;
}
else if(count%2==1 && secondList==null)
{
break;
}
prev=root;
root=root.next;
count++;
}
while(firstList!=null)
{
head.val=firstList.val;
head=head.next;
firstList=firstList.next;
}
}
}