[LintCode] 599 Insert into a Cyclic Sorted List 解题报告

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 }
时间: 2025-01-02 16:32:16

[LintCode] 599 Insert into a Cyclic Sorted List 解题报告的相关文章

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

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. (increasing sorted) We need to consider the f

LeetCode: Search in Rotated Sorted Array 解题报告

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no

Remove Duplicates from Sorted List解题报告

---恢复内容开始--- Remove Duplicates from Sorted List 题目意图:删除掉linked list里面的重复的元素 思路: 1. 对于重复的点,会保留当前数字第一次出现的点,所以返回结果的起始点与当前起始点为同一个点,不需要新建dummy node来表示起始点.     2. 用一个pre pointer遍历整个linked list,如果当前点与下一个点一致则删除下一个点. 我的代码: 1 public static ListNode deleteDupli

[LeetCode] Median of Two Sorted Arrays 解题报告

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Subscribe to see which companies asked this question Show Tags 分析: 这个题目在leetco

【LeetCode】Merge k Sorted Lists 解题报告

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 合并几个有序链表为一个,分析算法复杂度. [分治] 直观的想法是两两合并,有两种方法:1)list1和list2合并为newlist2,newlist2再和list3合并为newlist3,newlist3再和list4合并为newlist4--依次类推:2)list1和list2合并为li

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

[leetcode] 88. Merge Sorted Array 解题报告

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi