成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

leetcode86. Partition List

layman / 3385人閱讀

摘要:當(dāng)前節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)插入位置的前一個(gè)節(jié)點(diǎn),以及記錄初始位置的節(jié)點(diǎn)。當(dāng)發(fā)現(xiàn)一個(gè)需要交換的節(jié)點(diǎn)時(shí),先獲得這個(gè)節(jié)點(diǎn),然后將指向節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)。最后將兩個(gè)鏈表連接。代碼相比于第一種更加清晰一些。

題目要求
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

將小于x的值放在前面,大于等于x的值放在后面,移動(dòng)的過(guò)程中不改變數(shù)字之間的相對(duì)順序。

思路一:移動(dòng)節(jié)點(diǎn)

該思路需要我們記錄3個(gè)節(jié)點(diǎn)。當(dāng)前節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)currentPrev,插入位置的前一個(gè)節(jié)點(diǎn)prev,以及記錄初始位置的節(jié)點(diǎn)start。當(dāng)發(fā)現(xiàn)一個(gè)需要交換的節(jié)點(diǎn)時(shí),先獲得這個(gè)節(jié)點(diǎn),然后將currentPrev指向節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)。之后將當(dāng)前的節(jié)點(diǎn)插入到prev之后。代碼如下:

    public ListNode partition(ListNode head, int x) {
        if(head==null || head.next==null){
            return head;
        }
        ListNode start = new ListNode(0);
        ListNode prev = new ListNode(0);
        ListNode currentPrev = new ListNode(0);
        start.next = prev;
        prev.next = head;
        currentPrev.next = head;
        while(currentPrev.next!=null && currentPrev.next.val
思路二:使用兩個(gè)鏈表

我們?cè)O(shè)置兩個(gè)頭指針當(dāng)做兩個(gè)鏈表,當(dāng)遇到的數(shù)值小于x,則加入第一個(gè)鏈表,否則加入第二個(gè)鏈表。最后將兩個(gè)鏈表連接。代碼相比于第一種更加清晰一些。

    public ListNode partition2(ListNode head, int x) {
        if (head == null || head.next == null) return head;
        
        ListNode dummy1 = new ListNode(0);
        ListNode dummy2 = new ListNode(0);
        ListNode curr = head, first = dummy1, second = dummy2;
        while (curr != null) {
            if (curr.val < x) {
                first.next = curr;
                first = first.next;
            } else {
                second.next = curr;
                second = second.next;
            }
            curr = curr.next;
        }
        first.next = dummy2.next;
        second.next = null;
        return dummy1.next;
    }


想要了解更多開(kāi)發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號(hào)!將會(huì)不定期的發(fā)放福利哦~

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/67328.html

相關(guān)文章

  • [LeetCode] 86. Partition List

    Problem Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in ea...

    Yuqi 評(píng)論0 收藏0
  • [LeetCode] 763. Partition Labels

    Problem A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers represe...

    iliyaku 評(píng)論0 收藏0
  • [LintCode/LeetCode] Partition List

    摘要:新建兩個(gè)鏈表,分別存和的結(jié)點(diǎn)。令頭結(jié)點(diǎn)分別叫作和,對(duì)應(yīng)的指針?lè)謩e叫作和。然后遍歷,當(dāng)小于的時(shí)候放入,否則放入。最后,讓較小值鏈表尾結(jié)點(diǎn)指向較大值鏈表頭結(jié)點(diǎn),再讓較大值鏈表尾結(jié)點(diǎn)指向。 Problem Given a linked list and a value x, partition it such that all nodes less than x come before no...

    崔曉明 評(píng)論0 收藏0
  • [Leetcode] Palindrome Partitioning 回文分割

    摘要:深度優(yōu)先搜素復(fù)雜度時(shí)間空間思路因?yàn)槲覀円祷厮锌赡艿姆指罱M合,我們必須要檢查所有的可能性,一般來(lái)說(shuō)這就要使用,由于要返回路徑,仍然是典型的做法遞歸時(shí)加入一個(gè)臨時(shí)列表,先加入元素,搜索完再去掉該元素。 Palindrome Partitioning Given a string s, partition s such that every substring of the parti...

    leanote 評(píng)論0 收藏0

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

0條評(píng)論

閱讀需要支付1元查看
<