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?