forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloneWithRandPointers.java
75 lines (67 loc) · 2.55 KB
/
CloneWithRandPointers.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
73
74
75
package com.rampatra.linkedlists;
import com.rampatra.base.DoubleLinkedList;
import com.rampatra.base.DoubleLinkedNode;
/**
* Find the <a href="http://www.geeksforgeeks.org/a-linked-list-with-next-and-arbit-pointer/">question here</a>.
*
* @author rampatra
* @since 6/20/15
*/
public class CloneWithRandPointers {
/**
* Clones a linked list with next pointer pointing to the
* next node and prev pointer pointing to any random node.
*
* @param list
* @param <E>
* @return
*/
public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedList<E> list) {
DoubleLinkedNode<E> firstNode = list.getNode(0);
DoubleLinkedNode<E> curr = firstNode;
// copy each node and insert after it
while (curr != null) {
curr.next = new DoubleLinkedNode<>(null, curr.item, curr.next);
curr = curr.next.next;
}
// copy all random pointers from original node to the copied node
curr = firstNode;
while (curr != null && curr.next != null) {
curr.next.prev = (curr.prev != null) ? curr.prev.next : null;
curr = curr.next.next;
}
// separate the copied nodes into a different linked list
curr = firstNode;
DoubleLinkedNode<E> cloneHead = firstNode.next;
DoubleLinkedNode<E> dupNode;
while (curr != null && curr.next != null) {
dupNode = curr.next;
curr.next = dupNode.next;
dupNode.next = (curr.next != null) ? curr.next.next : null;
curr = curr.next;
}
return DoubleLinkedList.getLinkedList(cloneHead);
}
public static void main(String[] args) {
DoubleLinkedList<Integer> linkedList = new DoubleLinkedList<>();
linkedList.add(0);
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.getNode(0).prev = null;
linkedList.getNode(1).prev = linkedList.getNode(2);
linkedList.getNode(2).prev = linkedList.getNode(0);
linkedList.getNode(3).prev = linkedList.getNode(1);
System.out.println("======Original======");
linkedList.printList();
DoubleLinkedList<Integer> clonedList = clone(linkedList);
System.out.println("======Cloned======");
clonedList.printList();
System.out.println("======Cloned (Modified)======");
clonedList.set(0, 234);
clonedList.set(1, 567);
clonedList.printList();
System.out.println("======Original======");
linkedList.printList();
}
}