708. Insert into a Cyclic Sorted List
708. Insert into a Cyclic Sorted List Insert into a Cyclic Sorted List https://www.youtube.com/watch?v=8y5tYB004ss https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/discuss/149374/Java-5ms-One-Pass-and-Two-Pass-Traverse-With-Detailed-Comments-and-Edge-cases! 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. class Solution { public Node insert(Node start, int x) { // first condition has nothing to do with x val // second condition used to compare with x val , see if it fits // when there is no node, we add x , and make it circular if(start == null){ Node node = new Node(x, null); node.next = node; return node; } // there is at least one node, insert the x into correct pos Node cur = start; // use cur to move around , since we need to return start // as head at the end while(true){ // we make sure to get all cases covered and // break when we are done inserting // here we check every cur pos when we move // the cur pos can be at normal pos, where cur.next.val is bigger than cur.val // or the cur can be at the max, so cur.next.val is smaller than cur.val // or we don't know where the cur is at, when cur.next.val = cur.val // so we need to move more to find out, if it changes later // (1) see if its fits for x , when x is in between nodes in the middle // the cur pos can be at normal pos, where cur.next.val is bigger than cur.val // (2) see if it fits for x when x is the new max or new min // or the cur can be at the max, so cur.next.val is smaller than cur.val // (3) when all nodes in the list have the same value // which we didn't know until we done traversing all the nodes. // when we are back to the start node. // so we add the node after we done traversing all the nodes // or we don't know where the cur is at, when cur.next.val = cur.val // so we need to move more to find out, if it changes later // case (1) // we are at a normal pos if(cur.val < cur.next.val){ // see if x is the fit in this normal pos if(cur.val <= x && x <= cur.next.val){ insertAfter(cur, x); break; } // we are at max , its good for the new max or the new min // since new max is after the max now, // new min is also after the max now }else if( cur.val > cur.next.val){ // check if the x is the new max or new min // or at least equal to the min now or the max now if(x >= cur.val || x <= cur.next.val){ insertAfter(cur, x); break; } }else{ // when cur.val = cur.next.val // we don't know where we are, so we keep moving // unless we have travled all the nodes,and now are bacck, // very close to start, then we can add the node, because it means // we dind't stop at any nodes, because they are all equal // if we haven't reached the start, keep moving if(cur.next == start){ insertAfter(cur, x); break; } } // none of the above three cases meet, go to the next node cur = cur.next; } return start; } private void insertAfter(Node cur, int val){ Node next = cur.next; Node newNode = new Node(val); cur.next = newNode; newNode.next = next; } }
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.
posted on 2018-08-28 20:44 猪猪🐷 阅读(300) 评论(0) 收藏 举报
浙公网安备 33010602011771号