【Lintcode】096.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.

Example

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

题解:

Solution 1 ()

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        ListNode* left = new ListNode(-1);
        ListNode* right = new ListNode(-1);
        ListNode* l = left;
        ListNode* r = right;
        while (head) {
            if (head->val < x) {
                l->next = head;
                l = l->next;
            } else {
                r->next = head;
                r = r->next;
            }
            head = head->next;
        }
        l->next = right->next;
        r->next = nullptr;

        return left->next;
    }
};
时间: 2024-08-10 15:09:31

【Lintcode】096.Partition List的相关文章

【CF830C】Bamboo Partition 分块

[CF830C]Bamboo Partition 题解:给你n个数a1,a2...an和k,求最大的d使得$\sum\limits_{i=1}^n((d-a[i] \% d) \% d) \le k$ n<=100,a[i]<=10^9,k<=10^11 题解:$\sum\limits_{i=1}^n((d-a[i] \% d) \% d)=d\sum\limits_{i=1}^n{\lceil {a[i]\over d}\rceil }-\sum\limits_{i=1}^na[i]$

【CF932G】Palindrome Partition 回文自动机

[CF932G]Palindrome Partition 题意:给你一个字符串s,问你有多少种方式,可以将s分割成k个子串,设k个子串是$x_1x_2...x_k$,满足$x_1=x_k,x_2=x_{k-1}...x_i=x{k-i+1}$. $|s|\le 10^6$ 题解:设字符串的长度为n,考虑字符串$T=s_1s_ns_2s_{n-1}...$.问题就转化成了:求将原串划分成若干个长度为偶数的回文子串的方案数. 首先我们有一种暴力的想法,设f[i]表示将前i个字符分成若干个回文子串的方

【Lintcode】136.Palindrome Partitioning

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example Given s = "aab", return: [ ["aa","b"], ["a","a","

【Lintcode】074.First Bad Version

题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version. You can call isBa

【Lintcode】Median of two Sorted Arrays

class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: a double whose format is *.5 or *.0 */ int check(vector<int>& A, vector<int>& B,int k,int m) { int M = A.size(); int N = B.size(); int

【Oracle】OVER(PARTITION BY)函数用法

OVER(PARTITION BY)函数介绍 开窗函数               Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行. 开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:1:over后的写法:       over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数   over(pa

【原创】leetCodeOj ---Partition List 解题报告

原题地址: https://oj.leetcode.com/problems/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

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

【leetcode】86 partition list

题目说明 https://leetcode-cn.com/problems/partition-list/description/ 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 解法1 遍历链表,记录最后一个小于x的结点less,当遍历到的结点小于x时,将该点插入到less之后,直到遍历结束. /* * 时间复杂度:O(n) * 记录最后一低点小于x的点less * 遍历链表,当遍历到的点(p