comments | edit_url |
---|---|
true |
给定循环升序列表中的一个点,写一个函数向这个列表中插入一个新元素 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 * 10^4
-10^6 <= Node.val <= 10^6
-10^6 <= insertVal <= 10^6
注意:本题与主站 708 题相同: https://leetcode.cn/problems/insert-into-a-sorted-circular-linked-list/
"""
# 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':
node = Node(insertVal)
if head is None:
node.next = node
return node
p = head
while True:
if (
p.val <= insertVal
and insertVal <= p.next.val
or p.val > p.next.val
and (insertVal <= p.next.val or insertVal >= p.val)
or p.next == head
):
node.next = p.next
p.next = node
break
p = p.next
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 p = head;
for (;;) {
if (p.val <= insertVal && insertVal <= p.next.val
|| p.val > p.next.val && (insertVal <= p.next.val || insertVal >= p.val)
|| p.next == head) {
node.next = p.next;
p.next = node;
break;
}
p = p.next;
}
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
}
/**
* Definition for node.
* class Node {
* val: number
* next: Node | null
* constructor(val?: number, next?: Node) {
* this.val = (val===undefined ? 0 : val);
* this.next = (next===undefined ? null : next);
* }
* }
*/
function insert(head: Node | null, insertVal: number): Node | null {
const newNode = new Node(insertVal);
if (head == null) {
newNode.next = newNode;
return newNode;
}
const dummy = new Node(0, head);
let cur = dummy.next;
while (cur.next != dummy.next) {
const val = cur.val;
const nextVal = cur.next.val;
if (val > nextVal) {
if (
(insertVal >= val && insertVal >= nextVal) ||
(insertVal <= val && insertVal <= nextVal)
) {
break;
}
} else {
if (insertVal >= val && insertVal <= nextVal) {
break;
}
}
cur = cur.next;
}
newNode.next = cur.next;
cur.next = newNode;
return dummy.next;
}
/* class Node {
* var val: Int
* var next: Node?
* init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/
class Solution {
func insert(_ head: Node?, _ insertVal: Int) -> Node? {
let newNode = Node(insertVal)
if head == nil {
newNode.next = newNode
return newNode
}
var current = head
repeat {
if current!.val <= insertVal && insertVal <= current!.next!.val {
break
}
if current!.val > current!.next!.val && (insertVal >= current!.val || insertVal <= current!.next!.val) {
break
}
if current!.next === head {
break
}
current = current!.next
} while current !== head
newNode.next = current!.next
current!.next = newNode
return head
}
}