Advertisement
enigmjoe

Big O week 4 day 1 p2

Sep 4th, 2023
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. Problem Link: https://www.codingninjas.com/studio/problems/clone-a-linked-list-with-random-pointers_983604?leftPanelTab=1
  2. Author: Sarthak Joleya
  3.  
  4. public class Solution {
  5.     public static Node cloneLL(Node head) {
  6.         Node ans = new Node(-1) , tmp = head;
  7.         Node prev = ans;
  8.         while(tmp != null){
  9.             Node x = new Node(tmp.data);
  10.             prev.next = x;
  11.             prev = x;
  12.             tmp = tmp.next;
  13.         }
  14.         prev = ans.next;
  15.         tmp = head;
  16.         while(tmp != null){
  17.             prev.random = tmp.random;
  18.             prev = prev.next;
  19.             tmp = tmp.next;
  20.         }
  21.         return ans.next;
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement