Total Accepted: 53879 Total Submissions: 190701 Difficulty: Medium

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.

/**
* 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* pivot = NULL;
ListNode* cur = head;
ListNode* insert_pos = cur;
ListNode* insert_pos_pre = NULL;
while(cur && cur->val < x){
insert_pos_pre = cur;
cur = cur->next;
insert_pos = cur;
}
pivot = cur; /*
pivot 左边做插入,insert_node_pre,inser_node
pivot 右边做删除, delete_node_pre,delete_node
*/ ListNode* possible_delete_node_pre = pivot;
ListNode* possible_delete_node = pivot ? pivot->next : NULL; while(possible_delete_node){
ListNode* possible_delete_node_next = possible_delete_node->next;
if(possible_delete_node->val < x){
if(insert_pos_pre){
insert_pos_pre->next = possible_delete_node ;
}else{
head = possible_delete_node;
}
possible_delete_node->next = insert_pos; insert_pos_pre = possible_delete_node;
insert_pos = insert_pos_pre->next; possible_delete_node_pre ->next = possible_delete_node_next; }else{
possible_delete_node_pre = possible_delete_node;
}
possible_delete_node = possible_delete_node_next;
} return head; }
};

[Linked List]Partition List的更多相关文章

  1. [LeetCode] Partition List 划分链表

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

  2. leetcode 86. 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 List

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

  4. 【leetcode】Partition List(middle)

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

  5. Leetcode Partition List

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

  6. 46. Partition List

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

  7. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  8. [LeetCode]题解(python):086 - Partition List

    题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...

  9. 86. Partition List

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

随机推荐

  1. SSH连接LINUX乱码解决方法

    1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030" LANGUAGE="zh_CN.GB18030:zh_CN.GB2 ...

  2. jQuery插件开发入门

    扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间.这篇文章将概述jQuery插件开发的基本知识,最佳做法和常见的陷阱. 入门 编写一个jQuery插件开始于给jQuery.fn加入 ...

  3. 发布(Windows)

    发布(Windows) 本篇将在这个系列演示的例子上继续记录Asp.Net Core在Windows上发布的过程. Asp.Net Core在Windows上可以采用两种运行方式.一种是自托管运行,另 ...

  4. linq分组查询

    string[] arrStr = { ".com", "www.baidu.com", "www.qq.com", "www.b ...

  5. 如何不保存Word即可用Word打开想显示的内容!

    今天遇到客户需要将数据用Word预览,原来做的时候是保存到本地然后打开,客户说这样不好,因为不一定预览了一定要保存,于是我翻箱倒柜的发现真没资料啊!!幸好经过我的大量实验(好吧,我是挨个方法试的!), ...

  6. VS Visual Studio connection(); Microsoft Visulal Studio vNext & Azure

    http://www.visualstudio.com/connect-event-vs

  7. 把程序嵌入网页之ATL编写ActiveX[标准窗口+接受参数]

    从VS2010开始ATL ActiveX支持IObjectSafety接口,所以用VS2010来编写,新建一个ATL项目 向导的第一页没什么东西,直接下一步,选项可以根据具体需求调整 点“完成”,切换 ...

  8. oracle 查询表名以及表的列名

    oracle 查询表名以及表的列名的代码.   1.查询表名: 代码如下: select table_name,tablespace_name,temporary from user_tables [ ...

  9. Android 属性动画(Property Animation) 全然解析 (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38092093 上一篇Android 属性动画(Property Animatio ...

  10. R学习笔记

    把学习过程记载下来,加深印象,到时要是忘了也容易查,有需要的同学也可以参考: 1.包的安装:两种方法:一种通过R的菜单,先设定cran镜像,然后安装程序包,会出来一个列表,选择相应程序包安装,安装完毕 ...