-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
48 lines (43 loc) · 1.36 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// github.com/RodneyShag
/*
* For your reference:
*
* DoublyLinkedListNode {
* int data;
* DoublyLinkedListNode next;
* DoublyLinkedListNode prev;
* }
*
*/
// Time Complexity: O(n)
// Space Complexity: O(1)
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) {
/* Create Node to insert */
DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
if (head == null) { // insert in empty list
return newNode;
} else if (data < head.data) { // insert in front of list
newNode.next = head;
head.prev = newNode;
return newNode;
} else {
/* Walk list with 2 pointers (code is cleaner than using just 1 pointer) */
DoublyLinkedListNode n1 = null;
DoublyLinkedListNode n2 = head;
while (n2 != null && n2.data < data) {
n1 = n2;
n2 = n2.next;
}
if (n2 == null) { // insert at end of list
n1.next = newNode;
newNode.prev = n1;
} else { // insert somewhere within the list
n1.next = newNode;
n2.prev = newNode;
newNode.prev = n1;
newNode.next = n2;
}
return head;
}
}
// Discuss on HackerRank: https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/forum/comments/255744