Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 88cfd30

Browse files
author
Ram swaroop
committed
clone linkedlist with random pointers done (optimization pending)
1 parent 72c2687 commit 88cfd30

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/me/ramswaroop/common/DoubleLinkedList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ public void printList() {
188188
}
189189
out.println(curr.item + "]");
190190
// prints the list from last node
191-
/*out.print("[");
191+
out.print("[");
192192
while (curr.prev != null) {
193193
out.print(curr.item + ",");
194194
curr = curr.prev;
195195
}
196-
out.println(curr.item + "]");*/
196+
out.println(curr.item + "]");
197197
}
198198

199199
public static <E extends Comparable<E>> DoubleLinkedList<E> getLinkedList(DoubleLinkedNode<E> node) {

src/me/ramswaroop/linkedlists/CloneWithRandPointers.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,31 @@ public class CloneWithRandPointers<E extends Comparable<E>> extends DoubleLinked
1515
public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedNode<E> node) {
1616
DoubleLinkedNode<E> curr = node;
1717

18-
// copy node and insert after it
19-
while (curr != null && curr.next != null) {
18+
// copy each node and insert after it
19+
while (curr != null) {
2020
DoubleLinkedNode<E> newNode = new DoubleLinkedNode<>(null, curr.item, curr.next);
2121
curr.next = newNode;
2222
curr = curr.next.next;
2323
}
2424

25+
// copy all random pointers from original node to the copied node
2526
curr = node;
26-
27-
// copy all random pointers
2827
while (curr != null && curr.next != null) {
29-
curr.next.prev = curr.prev;
28+
curr.next.prev = (curr.prev != null) ? curr.prev.next : null;
3029
curr = curr.next.next;
3130
}
3231

32+
// separate the copied nodes into a different linked list
3333
curr = node;
3434
DoubleLinkedNode<E> cloneHead = node.next;
35-
DoubleLinkedNode<E> dupNode;
36-
// separate the copy nodes into a different linked list
37-
while (curr != null) {
35+
DoubleLinkedNode<E> dupNode = cloneHead;
36+
while (curr != null && curr.next != null) {
3837
dupNode = curr.next;
3938
curr.next = (dupNode != null) ? dupNode.next : null;
40-
if (dupNode != null) {
41-
dupNode.next = (curr.next != null) ? curr.next.next : null;
42-
}
39+
dupNode.next = (curr.next != null) ? curr.next.next : null;
4340
curr = curr.next;
4441
}
42+
4543
return DoubleLinkedList.getLinkedList(cloneHead);
4644
}
4745

@@ -51,13 +49,14 @@ public static void main(String a[]) {
5149
linkedList.add(11);
5250
linkedList.add(22);
5351
linkedList.add(33);
54-
linkedList.getNode(0).prev = linkedList.getNode(2);
55-
linkedList.getNode(1).prev = linkedList.getNode(3);
52+
linkedList.getNode(0).prev = null;
53+
linkedList.getNode(1).prev = linkedList.getNode(2);
5654
linkedList.getNode(2).prev = linkedList.getNode(0);
5755
linkedList.getNode(3).prev = linkedList.getNode(1);
5856
linkedList.printList();
5957
DoubleLinkedList<Integer> clonedList = clone(linkedList.getNode(0));
6058
clonedList.size = 4;
59+
clonedList.set(0, 234);
6160
clonedList.set(1, 567);
6261
clonedList.printList();
6362
linkedList.printList();

0 commit comments

Comments
 (0)