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.

class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head == NULL || head->next == NULL) return head;
ListNode* p = new ListNode(), *pre_p = p;
ListNode *q = new ListNode(), *pre_q = q;
while(head){
if(head->val < x){
ListNode *tmp = head->next;
head->next = pre_p->next;
pre_p->next = head;
pre_p = pre_p->next;
head=tmp;
}else{
ListNode *tmp = head->next;
head->next = pre_q->next;
pre_q->next = head;
pre_q = pre_q->next;
head=tmp;
}
}
pre_p->next = q->next;
return p->next;
}
};

Leetcode Partition List的更多相关文章

  1. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  2. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  3. [LeetCode] Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  4. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  5. [leetcode]Partition List @ Python

    原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...

  6. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

  7. LeetCode - Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  8. LeetCode: Partition List 解题报告

    Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...

  9. Leetcode: Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. location url 反向代理到来机的其它端口 gitlab

    location /nexus { proxy_pass http://127.0.0.1:8081/nexus; } [root@GitMaven conf]# pwd /var/opt/gitla ...

  2. 清空mysql的历史记录

    # vi ~/.mysql_history show tables; show databases; 清空里面的内容,并不用退出当前shell,就可以清除历史命令!!

  3. EF – 2.EF数据查询基础(上)查询数据的实用编程技巧

    目录 5.4.1 查询符合条件的单条记录 EF使用SingleOrDefault()和Find()两个方法查询符合条件的单条记录. 5.4.2 Entity Framework中的内部数据缓存 DbS ...

  4. 与你相遇好幸运,Tippecanoe在Centos下の安装

    全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...

  5. Java并发编程中的阻塞和中断

    >>线程的状态转换 线程的状态转换是线程控制的基础,下面这张图片非常直观的展示了线程的状态转换: 线程间的状态转换: 1. 新建(new):新创建了一个线程对象.2. 可运行(runnab ...

  6. [LeetCode] Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. .NET yield

    .Net Yield 其实比较简单,手动yield,一学就会. public static class GalaxyClass { public static void ShowGalaxies() ...

  8. hdu 4044 2011北京赛区网络赛E 树形dp ****

    专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...

  9. String之-如何取得精确byte长度字符串

    背景:公司生产线上出现异常,报的错是记录日志时数据库长度超出,导致异常,经查询发现是由于在计算byte长度时出了问题. 问题代码: operatorLog.setOperAfterData(updat ...

  10. VMware报错:“device eth0 does not seem to be present, delaying initialization ”

    转自:http://blog.sina.com.cn/s/blog_77126fa501018s3d.html vmlite虚拟机启动出错,就把这个虚拟机删除掉重新建立,系统虚拟硬盘使用之前的,启动系 ...