-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0708-InsertIntoASortedCircularLinkedList.cs
78 lines (66 loc) · 1.84 KB
/
0708-InsertIntoASortedCircularLinkedList.cs
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
68
69
70
71
72
73
74
75
76
77
78
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 25.2 MB
// Link: https://leetcode.com/submissions/detail/399393656/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/*
// Definition for a Node.
public class Node {
public int val;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
next = null;
}
public Node(int _val, Node _next) {
val = _val;
next = _next;
}
}
*/
public class _0708_InsertIntoASortedCircularLinkedList
{
public Node Insert(Node head, int insertVal)
{
if (head == null)
{
Node newNode = new Node(insertVal, null);
newNode.next = newNode;
return newNode;
}
Node prev = head;
Node curr = head.next;
do
{
if (prev.val <= insertVal && insertVal <= curr.val)
break;
else if (prev.val > curr.val)
if (insertVal >= prev.val || insertVal <= curr.val)
break;
prev = curr;
curr = curr.next;
} while (prev != head);
prev.next = new Node(insertVal, curr);
return head;
}
public class Node
{
public int val;
public Node next;
public Node() { }
public Node(int _val)
{
val = _val;
next = null;
}
public Node(int _val, Node _next)
{
val = _val;
next = _next;
}
}
}
}