-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReorderList.java
72 lines (57 loc) · 1.59 KB
/
ReorderList.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*Given a singly linked list: A0→A1→…→An-1→An, reorder it to: A0→An→A1→An-1→A2→An-2→…
For example: Given 1->2->3->4->5 its reorder is 1->5->2->4->3.
Note: It is recommended do this in-place without altering the nodes' values.
Input:
First line of input contains number of testcases T. For each testcase, there will be two lines of input, first of which contains length of linked list and next line contains elements of the nodes of linked list.
Output:
Reorder it as explained above.
User Task:
The task is to complete the function reorderList() which should reorder the list as required.
Constraints:
1 <= T <= 200
1 <= N <= 200
Example:
Input:
2
3
1 2 3
4
1 7 3 4
Output:
1 3 2
1 4 7 3
Explanation:
Testcase 2: After rearranging the given list as required, we have list as 1 -> 4 -> 7 -> 3.*/
class saksham
{
Node reorderlist(Node head)
{
Node ref=head;
ArrayList<Integer> list = new ArrayList<Integer>();
while(ref!=null)
{
list.add(ref.data);
ref=ref.next;
}
Node result=new Node(-1);
Node pointer=result;
int n=list.size();
int m=0;
int o=n-1;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
pointer.next=new Node(list.get(m));
m++;
pointer=pointer.next;
}
else{
pointer.next=new Node(list.get(o));
o--;
pointer=pointer.next;
}
}
return result.next;
}
}