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.
Answer in javascript
function sortedInsert(head, data) {
let newNode = new DoublyLinkedListNode(data);
// Handle empty list or insert at front
if (!head || data <= head.data) {
newNode.next = head;
if (head) {
head.prev = newNode;
}
return newNode;
}
let current = head;
while (current.next && current.next.data < data) {
current = current.next;
}
// Insert the new node
newNode.next = current.next;
if (current.next) {
current.next.prev = newNode;
}
current.next = newNode;
newNode.prev = current;
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 →
Answer in javascript function sortedInsert(head, data) { let newNode = new DoublyLinkedListNode(data);
}