-
Notifications
You must be signed in to change notification settings - Fork 0
/
m708.py
44 lines (37 loc) · 1.23 KB
/
m708.py
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
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, next=None):
self.val = val
self.next = next
"""
class Solution:
def insert(self, head: 'Optional[Node]', insertVal: int) -> 'Node':
if not head :
newHead = Node(insertVal)
newHead.next = newHead
return newHead
if head.next == head :
newNode = Node(insertVal, head)
head.next = newNode
return head
curr = head
cycle = False # Since we don't know if our entry point was past intended
while True :
if curr.next == head :
cycle = True
''' 1. proper place
2. largest val
3. smallest val
4. cycle found
'''
if (curr.val <= insertVal <= curr.next.val) or \
(curr.val < insertVal and curr.val > curr.next.val) or \
(insertVal < curr.next.val and curr.val > curr.next.val) or \
cycle :
newNode = Node(insertVal, curr.next)
curr.next = newNode
return head
else :
curr = curr.next
return None