-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathList shuffle.java
More file actions
44 lines (39 loc) · 821 Bytes
/
List shuffle.java
File metadata and controls
44 lines (39 loc) · 821 Bytes
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
/*
class Node {
Node next;
int data;
Node(int data) {
this.data = data;
next = null;
}
}
*/
public static Node Shuffle(Node head){
//Enter your code here
Node slow = head;
Node fast = slow.next;
boolean restricted = false;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
Node left=head;
Node right=slow.next;
slow.next=null;
Node dummy = new Node(0);
Node temp = dummy;
while(left!=null || right!=null){
if(left!=null){
temp.next=left;
temp=temp.next;
left=left.next;
}
if(right!=null){
temp.next=right;
temp=temp.next;
right=right.next;
}
}
head =dummy.next;
return head;
}