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. PHP学习笔记十八【构造函数】

    <?php class Person{ public $name; public $age; //定义构造函数 function 空格__construct 构造方法没有返回值,对象自动调用 p ...

  2. Oracle相关的知识点

    1. 如何在Oracle SQLPlus中执行SQL Script文件 以下面的格式在提示符中输入@{file name} SQL>@{file} 假设你要运行的文件的名字是script.sql ...

  3. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  4. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  5. git add和被ignore的文件

    如果有如下的目录结构: workspace tree | --------------------- |                             | hello.c           ...

  6. HTTP协议入门知识

    HTTP超文本传输协议,是客户端浏览器和服务器通信的规范,是浏览器与服务器通信的协议,属于应用层的协议,web开发者了解HTTP协议非常重要.浏览器通过http协议请求服务器,完成请求服务器立刻关闭连 ...

  7. Linq的基础2

    var 构建匿名类型1 = from c in ctx.GetTable<Customers>()                          select new          ...

  8. JSON 传值 textarea中虚拟换行功能

    遇到错误的袭击, 错误出现,使用jquery中ajax进行查询数据时执行完以后,需要把数据封装成为JSON类型的数据,并传递到前台去的时候出现Invalid JSON 错误,经查找后发现是在使用tex ...

  9. Js 导出Excel IE ActiveX控件

    function ExportExcel() { var oXL = new ActiveXObject("Excel.Application"); //创建excel应用程序对象 ...

  10. android listview Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

    android listview 适配器在多种类型viewType报错: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; ...