This solution is wrong but can pass the OJ, may be the test cases is too less

  • 0
    //I think there are some problem with this method
            if(head == null) return null;
            RandomListNode newHead = new RandomListNode(head.label);
            RandomListNode newCur = newHead, cur = head;
            while(cur != null){
                if(cur.random != null){
                    RandomListNode newNode = new RandomListNode(cur.random.label);
                    newCur.random = newNode;
                if( != null){
                    RandomListNode newNode = new RandomListNode(;
           = newNode;
                    newCur =;
                cur =;
            return newHead;

  • 1

    Yeah, I think the code may have a serious problem. The random pointer should points to the Linked List ifself. I guess the judge pass your code just because it doesn't judge whether the node which random pointers points is in the Linked List or not, the node's label instead. Thus, you will pass and got AC.

  • 0

    Good point, but this is not my code, my code is using HashMap, but I want to find a solution using constant space. This one is copied from others.

Log in to reply

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