-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay20_ChangeOrder.java
54 lines (46 loc) · 1.07 KB
/
Day20_ChangeOrder.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
class Solution{
public void reorderList(ListNode head) {
ListNode cur=head;
int count=0;
while(cur!=null)
{
count++;
cur=cur.next;
}
int term;
System.out.println("total nodes are"+count);
if(count%2==0)
term=count/2-1;
else
term=(count-1)/2;
int a=1;
for(int i=1;i<=term;i++)
{
head=change(head,a);
a=a+2;
}
}
private ListNode change(ListNode head,int count)
{
ListNode last=head,prev=null;
while(last.next!=null)
{
prev=last;
last=last.next;
}
ListNode tobechanged=head;
for(int i=1;i<count;i++)
{
if(count==1)
break;
tobechanged=tobechanged.next;
}
System.out.println("Changing "+tobechanged.val);
System.out.println("Changed with"+last.val);
ListNode temp=tobechanged.next;
tobechanged.next=last;
last.next=temp;
prev.next=null;
return head;
}
}