LeetCode 708. [Python] Insert into a Sorted Circular Linked List
原题链接
中等
作者:
徐辰潇
,
2021-05-28 07:19:06
,
所有人可见
,
阅读 395
"""
# 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: 'Node', insertVal: int) -> 'Node':
#TC: O(n)
#SC: O(1)
if head is None:
head = Node(insertVal)
head.next = head
return head
else:
p = head
while (p.next != head):
if p.next.val >= insertVal and p.val < insertVal:
break
if p.next.val < p.val:
if insertVal >= p.val or insertVal <= p.next.val:
break
p = p.next
newNode = Node(insertVal)
tmp = p.next
p.next = newNode
newNode.next = tmp
return head