Partition List -- leetcod

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.

基本思路:

1. 设置两个临时链表

2. 将小于x的节点,挂到一个链表上。将大于等于的节点,挂到另一个链表上。

3. 串接两个链表。将后一个链表的头部,挂以前一个链表的尾部。

所犯的错误:

初次提交时,漏写了

p2->next = 0;

提交后,报告运行时间超出。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        ListNode h1(0);
        ListNode h2(0);
        ListNode *p1 = &h1;
        ListNode *p2 = &h2;
        while (head) {
            if (head->val < x) {
                p1->next = head;
                p1 = p1->next;
            }
            else {
                p2->next = head;
                p2 = p2->next;
            }
            head = head->next;
        }
        p1->next = h2.next;
        p2->next = 0; // 此句漏写,将会在单链表中产生环。

        return h1.next;
    }
};

在leetcode讨论组中,有一个更简洁的写法。

https://leetcode.com/discuss/21032/very-concise-one-pass-solution

ListNode *partition(ListNode *head, int x) {
    ListNode node1(0), node2(0);
    ListNode *p1 = &node1, *p2 = &node2;
    while (head) {
        if (head->val < x)
            p1 = p1->next = head;
        else
            p2 = p2->next = head;
        head = head->next;
    }
    p2->next = NULL;
    p1->next = node2.next;
    return node1.next;
}

使用连续赋值,将两句写成了一句。

时间: 2024-09-29 03:57:49

Partition List -- leetcod的相关文章

[LeetCode]Partition List

题目描述:(链接) 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

doesn&#39;t contain a valid partition table 解决方法

输入 fdisk -l 可以看到 输入 fdisk /dev/xvdb 跟着向导一步步做下去(如果不知道该输入什么,就输入“m”并回车,可以打印出菜单): Command (m for help): m Command action a   toggle a bootable flag b   edit bsd disklabel c   toggle the dos compatibility flag d   delete a partition l   list known partiti

LeetCode --- 86. Partition List

题目链接:Partition List 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,

Leetcode:Partition List 链表快速排序划分

Partition List 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

Lintcode31 Partition Array solution题解

[题目描述] Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:All elements < k are moved to the left;All elements >= k are moved to the right;Return the partitioning index, i.e the fir

Partition算法以及其应用详解(Golang实现)

最近像在看闲书一样在看一本<啊哈!算法> 当时在amazon上面闲逛挑书,看到巨多人推荐这本算法书,说深入浅出简单易懂便买来阅读.实际上作者描述算法的能力的确令人佩服.就当复习常用算法吧. 后面会依次纪录一下我觉得有意思的常用算法使用,这次就是快排. 快速排序简介: 快排的中心思想还是二分法,通过partition算法,先将需要排序的数组分为两个部分,再用递归的思想反复这个过程.最后将排序好的最小单元再依次组装起来获得最后的数据.快排的平均时间复杂度是O(nlogN),最糟糕的情况是O(N平方

转载: scala中span和partition区别

scala中的partition span splitAt groupBy 可把Collection分成:满足条件的一组,其他的另一组. partitionspan List(1,9,2,4,5).span(_<3)       // (List(1),List(9, 2, 4, 5)),碰到不符合就结束 List(1,9,2,4,5).partition(_<3) // (List(1, 2),List(9, 4, 5)),扫描所有 splitAt // (List(1, 3),List(5

LeetCode86 Partition List

题目: 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.(Medium) For example,Given 1-

hive中partition如何使用

1.背景 1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念. 2.分区表指的是在创建表时指定的partition的分区空间. 3.如果需要创建有分区的表,需要在create表的时候调用可选参数partitioned by,详见表创建的语法结构. 2.细节 1.一个表可以拥有一个或者多个分区,每个分区以文件夹的形式单独存在表文件夹的目录下. show partitions stage_