Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Copy List with Random Pointer

Description

Flashcards on Copy List with Random Pointer, created by Suhas S on 08/03/2018.
Suhas S
Flashcards by Suhas S, updated more than 1 year ago
Suhas S
Created by Suhas S over 7 years ago
1
0
1 2 3 4 5 (0)

Resource summary

Question Answer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. public RandomListNode copyRandomList(RandomListNode head) { if(head == null) return head; RandomListNode next, copy, iter = head; while(iter!=null){ next = iter.next; copy = new RandomListNode(iter.label); iter.next = copy; copy.next = next; iter = next; } iter = head; while(iter !=null){ if(iter.random != null){ iter.next.random = iter.random.next; } iter = iter.next.next; } RandomListNode pseudoHead = new RandomListNode(0); RandomListNode copyIter = pseudoHead; iter = head; while(iter!=null){ next = iter.next.next; copy = iter.next; copyIter.next = copy; copyIter = copy; iter.next = next; iter = next; } return pseudoHead.next; }
Show full summary Hide full summary

0 comments

There are no comments, be the first and leave one below:

Similar

Delete Node in a BST
Suhas S
Delete a node with a key
Suhas S
Remove Nth Node From End of List
Suhas S
Swap Nodes in Pairs
Suhas S
Merge Two Sorted Lists
Suhas S
Odd Even Linked List
Suhas S
Rotate List
Suhas S
Add Two Numbers
Suhas S
Lord of the Flies - CFE Higher English
Daniel Cormack