国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 86. Partition List

Yuqi / 2722人閱讀

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 each of the two partitions.

Example:

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

Solution
class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(0);
        ListNode dummy2 = new ListNode(0);
        ListNode small = dummy1, large = dummy2;
        while (head != null) {
            if (head.val < x) {
                small.next = head;
                small = small.next;
            } else {
                large.next = head;
                large = large.next;
            }
            head = head.next;
        }
        small.next = dummy2.next;
        large.next = null;
        return dummy1.next;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72529.html

相關文章

  • leetcode86. Partition List

    摘要:當前節點的前一個節點插入位置的前一個節點,以及記錄初始位置的節點。當發現一個需要交換的節點時,先獲得這個節點,然后將指向節點的后一個節點。最后將兩個鏈表連接。代碼相比于第一種更加清晰一些。 題目要求 Given a linked list and a value x, partition it such that all nodes less than x come before no...

    layman 評論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 評論0 收藏0
  • [LintCode/LeetCode] Partition List

    摘要:新建兩個鏈表,分別存和的結點。令頭結點分別叫作和,對應的指針分別叫作和。然后遍歷,當小于的時候放入,否則放入。最后,讓較小值鏈表尾結點指向較大值鏈表頭結點,再讓較大值鏈表尾結點指向。 Problem Given a linked list and a value x, partition it such that all nodes less than x come before no...

    崔曉明 評論0 收藏0
  • [Leetcode] Palindrome Partitioning 回文分割

    摘要:深度優先搜素復雜度時間空間思路因為我們要返回所有可能的分割組合,我們必須要檢查所有的可能性,一般來說這就要使用,由于要返回路徑,仍然是典型的做法遞歸時加入一個臨時列表,先加入元素,搜索完再去掉該元素。 Palindrome Partitioning Given a string s, partition s such that every substring of the parti...

    leanote 評論0 收藏0

發表評論

0條評論

Yuqi

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<