You are viewing a single comment's thread. Return to all comments →
Java solution:
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) { DoublyLinkedListNode curNode = head; DoublyLinkedListNode insertNode = new DoublyLinkedListNode(data); while(curNode != null) { int nodeData = curNode.data; if(nodeData > data) { if(curNode.prev == null) { insertNode.next = curNode; curNode.prev = insertNode; head = head.prev; } else { DoublyLinkedListNode prevNode = curNode.prev; prevNode.next = insertNode; insertNode.prev = prevNode; insertNode.next = curNode; curNode.prev = insertNode; } break; } else if(curNode.next == null) { curNode.next = insertNode; insertNode.prev = curNode; break; } curNode = curNode.next; } return head; }
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 →
Java solution: