HELP!! I have no idea why my answer is wrong.


  • 0
    S

    /**

    • 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) {
      RandomListNode h =head;
      while(h!=null){
      RandomListNode curr = new RandomListNode(h.label);
      curr.next = h.next;
      h.next = curr;
      h = h.next.next;
      }

       h = head;
       
       while(h!=null){
           h.next.random = (h.next.random != null)? h.random.next : null;
           h = h.next.next;
       }
       
       RandomListNode dummyHead = new RandomListNode(0);
       RandomListNode temp = dummyHead;
       h = head;
       while(h!=null){
           temp.next = h.next;
           h.next = h.next.next;
           
           h = h.next;
           temp = temp.next;
      
       }
       return dummyHead.next;
      

      }
      }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.