lintcode

Posted yunyouhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode相关的知识,希望对你有一定的参考价值。

2017-9-27

Insert into a Cyclic Sorted List

Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the list. Return the inserted new node.
 
我的
1. node == null 时注意要自建环指向自己。
2.答案有问题??
技术分享
 1 public class Solution {
 2     /*
 3      * @param node: a list node in the list
 4      * @param x: An integer
 5      * @return: the inserted new list node
 6      */
 7     public ListNode insert(ListNode node, int x) {
 8         // write your code here
 9         if (node == null) {
10             ListNode node1 = new ListNode(x);
11             node1.next = node1;
12             return node1;
13         }
14         ListNode cur = node;
15         ListNode temp = node.next;
16         if (temp.val < x) {
17             cur = cur.next;
18             temp = temp.next;
19         }
20         ListNode newNode = new ListNode(x);
21         cur.next = newNode;
22         newNode.next = temp;
23         return temp;
24     }
25 }
View Code
30->50->2->2->3->5->7->9->11->20
2
50->2->2->2->3->5->7->9->11->20->30
我的错哪里了30->2->50->2->2->3->5->7->9->11->20
想简单了。





以上是关于lintcode的主要内容,如果未能解决你的问题,请参考以下文章

lintcode:排颜色 II

LintCode 413. 反转整数

LintCode 2. 尾部的零

lintcode 刷题:457 经典二分查找问题

LintCode 539: Move Zeroes

Lintcode 75.寻找峰值