给定循环单调非递减列表中的一个点,写一个函数向这个列表中插入一个新元素 insertVal
,使这个列表仍然是循环非降序的。
给定的可以是这个列表中任意一个顶点的指针,并不一定是这个列表中最小元素的指针。
如果有多个满足条件的插入位置,你可以选择任意一个位置插入新的值,插入后整个列表仍然保持有序。
如果列表为空(给定的节点是 null
),你需要创建一个循环有序列表并返回这个节点。否则,请返回原先给定的节点。
示例 1:
输入:head = [3,4,1], insertVal = 2 输出:[3,4,1,2] 解释:在上图中,有一个包含三个元素的循环有序列表,你获得值为 3 的节点的指针,我们需要向表中插入元素 2 。新插入的节点应该在 1 和 3 之间,插入之后,整个列表如上图所示,最后返回节点 3 。
示例 2:
输入:head = [], insertVal = 1
输出:[1]
解释:列表为空(给定的节点是 null
),创建一个循环有序列表并返回这个节点。
示例 3:
输入:head = [1], insertVal = 0 输出:[1,0]
提示:
0 <= Number of Nodes <= 5 * 104
-106 <= Node.val, insertVal <= 106
"""
# 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':
node = Node(insertVal)
if head is None:
node.next = node
return node
prev, curr = head, head.next
while curr != head:
if prev.val <= insertVal <= curr.val or (
prev.val > curr.val and (insertVal >= prev.val or insertVal <= curr.val)
):
break
prev, curr = curr, curr.next
prev.next = node
node.next = curr
return head
/*
// Definition for a Node.
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;
}
};
*/
class Solution {
public Node insert(Node head, int insertVal) {
Node node = new Node(insertVal);
if (head == null) {
node.next = node;
return node;
}
Node prev = head, curr = head.next;
while (curr != head) {
if ((prev.val <= insertVal && insertVal <= curr.val)
|| (prev.val > curr.val && (insertVal >= prev.val || insertVal <= curr.val))) {
break;
}
prev = curr;
curr = curr.next;
}
prev.next = node;
node.next = curr;
return head;
}
}
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node() {}
Node(int _val) {
val = _val;
next = NULL;
}
Node(int _val, Node* _next) {
val = _val;
next = _next;
}
};
*/
class Solution {
public:
Node* insert(Node* head, int insertVal) {
Node* node = new Node(insertVal);
if (!head) {
node->next = node;
return node;
}
Node *prev = head, *curr = head->next;
while (curr != head) {
if ((prev->val <= insertVal && insertVal <= curr->val) || (prev->val > curr->val && (insertVal >= prev->val || insertVal <= curr->val))) break;
prev = curr;
curr = curr->next;
}
prev->next = node;
node->next = curr;
return head;
}
};
/**
* Definition for a Node.
* type Node struct {
* Val int
* Next *Node
* }
*/
func insert(head *Node, x int) *Node {
node := &Node{Val: x}
if head == nil {
node.Next = node
return node
}
prev, curr := head, head.Next
for curr != head {
if (prev.Val <= x && x <= curr.Val) || (prev.Val > curr.Val && (x >= prev.Val || x <= curr.Val)) {
break
}
prev, curr = curr, curr.Next
}
prev.Next = node
node.Next = curr
return head
}