Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package algorithms.curated170.medium;

public class InsertIntoASortedCircularLinkedList {

public Node insert(Node head, int insertVal) {
Node inserted = new Node(insertVal);

if (head == null) {
inserted.next = inserted;
return inserted;
}

Node pre = head;
Node cur = head.next;

while (cur != head) {
if (pre.val <= insertVal && cur.val > insertVal) {
break;
}
else if (pre.val > cur.val && (insertVal >= pre.val || insertVal <= cur.val)) {
break;
}
pre = cur;
cur = cur.next;
}
pre.next = inserted;
inserted.next = cur;

return head;
}

class Node {
public int val;
public Node next;

public Node() {
}

public Node(int _val) {
val = _val;
}

public Node(int _val, Node _next) {
val = _val;
next = _next;
}
}
}