Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem Link: https://www.codingninjas.com/studio/problems/clone-a-linked-list-with-random-pointers_983604?leftPanelTab=1
- Author: Sarthak Joleya
- public class Solution {
- public static Node cloneLL(Node head) {
- Node ans = new Node(-1) , tmp = head;
- Node prev = ans;
- while(tmp != null){
- Node x = new Node(tmp.data);
- prev.next = x;
- prev = x;
- tmp = tmp.next;
- }
- prev = ans.next;
- tmp = head;
- while(tmp != null){
- prev.random = tmp.random;
- prev = prev.next;
- tmp = tmp.next;
- }
- return ans.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement