Skip to content

Commit cfdc7df

Browse files
committed
Create 0708-insert-into-a-sorted-circular-linked-list.java
1 parent af22904 commit cfdc7df

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public Node insert(Node head, int insertVal) {
3+
Node node = new Node(insertVal);
4+
if (head == null) {
5+
node.next = node;
6+
return node;
7+
}
8+
Node prev = head, curr = head.next;
9+
while (curr != head) {
10+
if ((prev.val <= insertVal && insertVal <= curr.val)
11+
|| (prev.val > curr.val && (insertVal >= prev.val || insertVal <= curr.val))){
12+
break;
13+
}
14+
prev = curr;
15+
curr = curr.next;
16+
}
17+
prev.next = node;
18+
node.next = curr;
19+
return head;
20+
}
21+
}

0 commit comments

Comments
 (0)