We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I too got this error.It is because,the value of data is not passed during the construction of newnode.Note that I have passed the value of newnode's data.
This java code worked for me:)
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) {
DoublyLinkedListNode n = new DoublyLinkedListNode(data);
n.data = data;
if(head == null)
{
return n;
}
else if (data <= head.data)
{
n.next=head;
head.prev=n;
return n;
}
else
{
DoublyLinkedListNode rest = sortedInsert(head.next,data);
head.next=rest;
rest.prev=head;
return head;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Inserting a Node Into a Sorted Doubly Linked List
You are viewing a single comment's thread. Return to all comments →
I too got this error.It is because,the value of data is not passed during the construction of newnode.Note that I have passed the value of newnode's data. This java code worked for me:) static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) { DoublyLinkedListNode n = new DoublyLinkedListNode(data); n.data = data; if(head == null) { return n; } else if (data <= head.data) { n.next=head; head.prev=n; return n; } else { DoublyLinkedListNode rest = sortedInsert(head.next,data); head.next=rest; rest.prev=head; return head; } }