-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInsertIntoSortedCircularList.java
34 lines (34 loc) · 1.13 KB
/
InsertIntoSortedCircularList.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
//Write a program to insert a value in a singly linked sorted circular list,
//given access to one of the nodes
//Input: [5->8->2] and Pointer to node [5] is given. Insert value [7].
//Output: [5->7->8->2]
class Solution {
public ListNode insertIntoSortedCircularList(ListNode head, int insertVal) {
ListNode node = new ListNode(insertVal);
if (head == null) {
node.next = node;
return node;
}
ListNode minHead = null;
ListNode tail = head;
while (tail.val < tail.next.val) {
tail = tail.next;
}
minHead = tail.next;
// 5->8->2 is now treated as 2->5->8
// if inserVal is minimum or maximum
if (insertVal < minHead.val || insertVal > tail.val) {
node.next = tail.next;
tail.next = node;
return head;
}
// if insertVal is somewhere in middle
ListNode curr = minHead;
while (curr.next.val < insertVal) {
curr = curr.next;
}
node.next = curr.next;
curr.next = node;
return head;
}
}