[LeetCode] 708. Insert into a Sorted Circular Linked List

Given a node from a Circular Linked List which is sorted in ascending order, write a function to insert a value insertVal into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list, and may not be necessarily the smallest value in the circular list.

If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.

If the list is empty (i.e., given node is null), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the original given node.

Example 1:


 

Input: head = [3,4,1], insertVal = 2
Output: [3,4,1,2]
Explanation: In the figure above, there is a sorted circular list of three elements. 
You are given a reference to the node with value 3, and we need to insert 2 into the
list. The new node should be inserted between node 1 and node 3. After the insertion,
the list should look like this, and we should still return node 3.

Example 2:

Input: head = [], insertVal = 1
Output: [1]
Explanation: The list is empty (given head is null). We create a new single circular 
list and return the reference to that single node.

Example 3:

Input: head = [1], insertVal = 0
Output: [1,0]

Constraints:

  • 0 <= Number of Nodes <= 5 * 10^4
  • -10^6 <= Node.val <= 10^6
  • -10^6 <= insertVal <= 10^6

循环有序列表的插入。题意是给一个val有序的环形链表,链表的head节点不一定是链表中值最小的节点,同时给一个insertVal,请你把它做成一个节点并塞入链表的合适位置,使得最后的链表依然有序。

这道题没什么算法,做法也有几种,我讲一个我能理解的思路。首先corner case是如果给的链表是空的话,就创建一个新的节点,val = insertVal然后next指针指向自己。

一般的case也分两种情况,首先需要找到链表中val最大的节点。其中一种情况是如果insertVal大于最大的节点max或者小于最小的节点min,则可以将这个节点连到max和min中间。但是如果insertVal不符合这个情况的话,则需要用while循环找到一个符合题意的位置把insertVal塞进去,并和他的前面和后面的节点连接好。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public Node insert(Node head, int insertVal) {
 3         // corner case
 4         if (head == null) {
 5             Node node = new Node();
 6             node.val = insertVal;
 7             node.next = node;
 8             return node;
 9         }
10 
11         // normal case
12         // 找到最大节点max,之后一个节点就是最小节点min
13         Node max = head;
14         while (max.next != head && max.val <= max.next.val) {
15             max = max.next;
16         }
17         Node min = max.next;
18         Node cur = min;
19         // 如果insertVal比max大或者比min小
20         if (insertVal > max.val || insertVal <= min.val) {
21             Node node = new Node();
22             node.val = insertVal;
23             node.next = min;
24             max.next = node;
25         } else {
26             while (cur.next.val < insertVal) {
27                 cur = cur.next;
28             }
29             Node node = new Node(insertVal, cur.next);
30             cur.next = node;
31         }
32         return head;
33     }
34 }

 

LeetCode 题目总结

posted @ 2020-09-24 13:51  CNoodle  阅读(193)  评论(0编辑  收藏  举报