#Hey, is this the correct way to create a doubly LinkedList?

1 messages · Page 1 of 1 (latest)

ivory valve
#
    Node constructDLL(int arr[]) {
        // Code here
        
        Node head = new Node(arr[0]);
        Node last = head;
        
        for(int i = 1; i < arr.length; i++) {
            Node temp = new Node(arr[i]);
            last.next = temp;
            last = last.next;
            last.prev = temp;
        }
        
        return head;
    }

Some people do this :-

    Node constructDLL(int arr[])
    {
        Node head=new Node(arr[0]);
        Node last=head;
        
        for(int i=1; i<arr.length; i++)
        {
            Node temp=new Node(arr[i]);
            last.next=temp;
            temp.prev=last;
            last=last.next;
        }
        
        return head;
    }

So which one is correct? and if both are correct then which one is better?

main bearBOT
#

This post has been reserved for your question.

Hey @ivory valve! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hybrid condor
#

your isn't correct. Just try it