Created
February 24, 2014 16:27
-
-
Save b27lu/9191571 to your computer and use it in GitHub Desktop.
copy list with random pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list with a random pointer. | |
* class RandomListNode { | |
* int label; | |
* RandomListNode next, random; | |
* RandomListNode(int x) { this.label = x; } | |
* }; | |
*/ | |
public class Solution { | |
public RandomListNode copyRandomList(RandomListNode head) { | |
if(head == null) | |
return head; | |
//insert a copy after each original node (ignore random pointer now) | |
RandomListNode current = head; | |
while(current != null){ | |
RandomListNode copy = new RandomListNode(current.label); | |
copy.next = current.next; | |
current.next = copy; | |
current = current.next.next; | |
} | |
//copy random pointer | |
current = head; | |
while(current != null){ | |
if(current.random != null) //take care, random pointer may be null | |
current.next.random = current.random.next; | |
else | |
current.next.random = null; | |
current = current.next.next; | |
} | |
//split original list and copy list | |
current = head; | |
RandomListNode newHead = current.next; | |
RandomListNode newCurrent = newHead; | |
while(newCurrent != null){ | |
current.next = newCurrent.next; | |
if(newCurrent.next != null) | |
newCurrent.next = newCurrent.next.next; | |
current = current.next; | |
newCurrent = newCurrent.next; | |
} | |
return newHead; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment