码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode

时间:2017-09-27 21:01:01      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:new   自己   write   img   logs   put   pre   open   alt   

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

标签:new   自己   write   img   logs   put   pre   open   alt   

原文地址:http://www.cnblogs.com/yunyouhua/p/7603412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!