LeetCode 708. Insert into a Cyclic Sorted List

Original title link here: https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/

topic:

Given a node from a cyclic linked list which is sorted in ascending order, write a function to insert a value into the list such that it remains a cyclic sorted 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 cyclic list.

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

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

The following example may help you understand the problem better:

 



In the figure above, there is a cyclic sorted 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 insert between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.

answer:

Position than before to find the upswing after a small majority. InsertVal look at the inflection point is not the biggest or the minimum value, it is added to the inflection point position.

If you have not head back to find instructions have been flat, it added to the front of the head.

Time Complexity: O(n).

Space: O(1).

AC Java:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public Node next;
 6 
 7     public Node() {}
 8 
 9     public Node(int _val,Node _next) {
10         val = _val;
11         next = _next;
12     }
13 };
14 */
15 class Solution {
16     public Node insert(Node head, int insertVal) {
17         if(head == null){
18             = head new new the Node (insertVal, null );
 . 19              head.next = head;
 20 is              return head;
 21 is          }
 22 is          
23 is          the Node CUR = head;
 24          the while ( to true ) {
 25              IF (Cur.Val < cur.next.val) {
 26                  // rising phase to find the previous position is smaller than the larger 
27                  IF (Cur.Val <= insertVal && insertVal <= cur.next.val) {
 28                      cur.next = new new the Node (insertVal, cur.next);
 29                      BREAK ;
30                  }
 31 is              } the else  IF (Cur.Val> cur.next.val) {
 32                  // inflection point, if the previous description also greatly insertVal a maximum, plus the position of the inflection point
 33                  // if the number is smaller than the described minimum insertVal, also added to the inflection point 
34 is                  IF (Cur.Val <= insertVal insertVal || <= cur.next.val) {
 35                      cur.next = new new the Node (insertVal, cur.next);
 36                      BREAK ;
 37 [                  }
 38 is              } the else {
 39                  // point is always equal to the 
40                  IF (cur.next == head) {
 41 is                     cur.next = new Node(insertVal, head);
42                     break;
43                 }
44             }
45             
46             cur = cur.next;
47         }
48         
49         return head;
50     }
51 }

 

Guess you like

Origin www.cnblogs.com/Dylan-Java-NYC/p/10972122.html