-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInsertSortedCircularLinkedList.cpp
67 lines (63 loc) · 2.46 KB
/
InsertSortedCircularLinkedList.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Problem: https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/
// Definition for a Node.
struct Node {
int val;
Node* next;
Node() {}
Node(int _val): val(_val), next(NULL) {}
Node(int _val, Node* _next): val(_val), next(_next) {}
};
class InsertSortedCircularLinkedList {
public:
Node* insert(Node* head, int insertVal) {
Node* newNode = new Node(insertVal);
// Case-I: Head is null.
if (head == nullptr) { head = newNode; newNode->next = newNode; }
// Case-II: Single node.
else if (head->next == head) {
head->next = newNode;
newNode->next = head;
} else {
// Use 2-Pointer approach.
Node* curr = head->next;
Node* prev = head;
while (newNode->next == nullptr) {
//cout << "P, C=" << prev->val << "," << curr->val << endl;
// Case-III: Element already exists.
if (curr->val == insertVal) {
prev->next = newNode;
newNode->next = curr;
}
// Case-IV: Element is a valid mid-entry.
// Eg. [1, 3, 5], [2]
else if (prev->val <= insertVal && insertVal <= curr->val) {
prev->next = newNode;
newNode->next = curr;
}
// Case V: Element is the highest number.
// Eg. [1, 3, 5], [6]
// curr = 1, prev = 5
else if (prev->val <= insertVal && curr->val <= insertVal && curr->val < prev->val) {
prev->next = newNode;
newNode->next = curr;
}
// Case VI: Element is the smallest number.
// Eg. [1, 3, 5], [0]
// curr = 1, prev = 5
else if (insertVal <= prev->val && insertVal <= curr->val && curr->val < prev->val) {
prev->next = newNode;
newNode->next = curr;
}
// Case VII: Insert anywhere.
else if (curr == head) {
prev->next = newNode;
newNode->next = curr;
}
prev = curr;
curr = curr->next;
}
}
// End: Return head.
return head;
}
};