ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 708. Insert into a Cyclic Sorted List

2019-06-04 10:51:14  阅读:336  来源: 互联网

标签:node Insert head 708 val Cyclic list Node next


原题链接在这里:https://leetcode.com/problems/insert-into-a-cyclic-sorted-list/

题目:

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.

题解:

上升阶段找比前大比后小的位置. 拐点看看insertVal是不是最大或者最小值, 是就加在拐点位置.

如果转回head还没有找到说明一直是平的, 就加在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 Node(insertVal, null);
19             head.next = head;
20             return head;
21         }
22         
23         Node cur = head;
24         while(true){
25             if(cur.val < cur.next.val){
26                 // 上升阶段找比前小比后大的位置
27                 if(cur.val<=insertVal && insertVal<=cur.next.val){
28                     cur.next = new Node(insertVal, cur.next);
29                     break;
30                 }
31             }else if(cur.val > cur.next.val){
32                 // 拐点,如果比前个还大大说明insertVal最大, 加在拐点位置
33                 // 如果比后个还小说明insertVal最小, 也加在拐点位置
34                 if(cur.val<=insertVal || insertVal<=cur.next.val){
35                     cur.next = new Node(insertVal, cur.next);
36                     break;
37                 }
38             }else{
39                 // 一直都是相等的点
40                 if(cur.next == head){
41                     cur.next = new Node(insertVal, head);
42                     break;
43                 }
44             }
45             
46             cur = cur.next;
47         }
48         
49         return head;
50     }
51 }

 

标签:node,Insert,head,708,val,Cyclic,list,Node,next
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/10972122.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有