成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Insert into a Cyclic Sorted List

Acceml / 1082人閱讀

Problem

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

Tags

Amazon Linked List

Solution
public class Solution {
    public ListNode insert(ListNode node, int x) {
        ListNode head = node;
        if (node == null) {
            node = new ListNode(x);
            node.next = node;
            return node;
        }
        if (node.next == head) {
            insertNode(node, x);
            return head;
        }
        while (node != null && node.next != null) {
            if (node.val < node.next.val) {
                if (node.val <= x && x <= node.next.val) {
                    insertNode(node, x);
                    break;
                }
            } else if (node.val > node.next.val) {
                if (x >= node.val || x <= node.next.val) {
                    insertNode(node, x);
                    break;
                }
            } else {
                if (node.next == head) {
                    insertNode(node, x);
                    break;
                }
            }
            node = node.next;
        }
        return head;
    }
    public void insertNode(ListNode head, int x) {
        ListNode node = new ListNode(x);
        node.next = head.next;
        head.next = node;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/68098.html

相關(guān)文章

  • [LeetCode] 708. Insert into a Cyclic Sorted List

    Problem 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 r...

    qpwoeiru96 評(píng)論0 收藏0
  • [LintCode] Insert Node in Sorted Linked List

    Problem Insert a node in a sorted linked list. Example Given list = 1->4->6->8 and val = 5. Return 1->4->5->6->8. Solution public class Solution { public ListNode insertNode(ListNode head, int val...

    littleGrow 評(píng)論0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

    摘要:建立兩個(gè)堆,一個(gè)堆就是本身,也就是一個(gè)最小堆另一個(gè)要寫一個(gè),使之成為一個(gè)最大堆。我們把遍歷過的數(shù)組元素對(duì)半分到兩個(gè)堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時(shí),確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...

    zxhaaa 評(píng)論0 收藏0
  • [LintCode/LeetCode] Merge Sorted Array

    Problem Given two sorted integer arrays A and B, merge B into A as one sorted array. Notice You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements ...

    summerpxy 評(píng)論0 收藏0
  • [LintCode] Merge Sorted Array II

    摘要:循環(huán)里最好先考慮和其中之一已經(jīng)處理完的情況,就直接順序放另一個(gè)沒處理完的即可。然后再在里展開方法。避免其中一個(gè)數(shù)組較小會(huì)浪費(fèi)效率的情況。丫把參數(shù)又換成了。。。 Problem Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5...

    寵來也 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<