1. 程式人生 > >[LintCode] 599 Insert into a Cyclic Sorted List 解題報告

[LintCode] 599 Insert into a Cyclic Sorted List 解題報告

blog same ted next rom list ron div ger

Description
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.


Notice
3->5->1 is a cyclic list, so 3 is next node of 1.
3->5->1 is same with 5->1->3


Example


Given a list, and insert a value 4:
3->5->1
Return 5->1->3->4

5/18/2017

算法班,未經驗證

先找到list的頭,再來循環找插入點。

 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 10 ListNode newNode = new ListNode(x); 11 if (node == null) { 12 newNode.next = newNode; 13 return newNode; 14 } else if (node.next == node) { 15 node.next = newNode; 16 newNode.next = node;
17 return node; 18 } 19 ListNode maxNode = node; 20 21 while (maxNode.next.val > maxNode.val) { 22 maxNode = maxNode.next; 23 } 24 if (maxNode.val < newNode.val || maxNode.next.val > newNode.val) { 25 newNode.next = maxNode.next; 26 maxNode.next = newNode; 27 return newNode; 28 } 29 30 ListNode node = maxNode.next; 31 while (node.next.val < newNode.val) { 32 node = node.next; 33 } 34 newNode.next = node.next; 35 node.next = newNode; 36 return newNode; 37 } 38 }

但是實際上可以在單純遍歷的時候找插入點

別人的答案

 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         
10         if (node == null) {
11             node = new ListNode(x);
12             node.next = node;
13             return node;
14         }
15         
16         ListNode head = node;
17         while (node != null && node.next != null) {
18             if (node.val < node.next.val) {
19                 if (node.val <= x && x <= node.next.val) {
20                     insertNode(node, x);
21                     break;
22                 }
23             }
24             else if (node.val > node.next.val) {
25                 if (x > node.val || x < node.next.val) {
26                     insertNode(node, x);
27                     break;
28                 }
29             }
30             else { // node.val == node.next.val
31                 if (node.next == head) {
32                     insertNode(node, x);
33                     break;
34                 }
35             }
36             node = node.next;
37         }
38         
39         return head;
40     }
41     
42     public void insertNode(ListNode node, int x) {
43         ListNode newNode = new ListNode(x);
44         newNode.next = node.next;
45         node.next = newNode;
46     }
47 }

[LintCode] 599 Insert into a Cyclic Sorted List 解題報告