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. asp.net mvc 访问.html文件

    把html页面放在除Views文件夹外的任意文件夹,如Htmls--123.html,url直接访问便可,如 http://yourname/htmls/123.html 可以在路由表中排出不需要路由 ...

  2. SQL Server一些常见却不太记得住的命令

    一.数据库大小查询 1. exec sp_spaceused '表名'          --(SQL统计数据,大量事务操作后可能不准)2. exec sp_spaceused '表名', true  ...

  3. PHP--变量部分知识点

    PHP全局变量 PHP全局变量作用域不同与C,在函数内部不可以使用全局变量,要在函数内部使用全局变量需要,global $var或者使用超全局变量数组$GLOBALS['var']. 静态变量 PHP ...

  4. 【solr专题之一】Solr快速入门

    一.Solr学习相关资料 1.官方材料 (1)快速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html,以自带的example项目快速介绍发Solr ...

  5. 关于反射中获取Fields,method,Construts简单介绍

    * getFields()与getDeclaredFields()区别:getFields()只能访问类中声明为公有的字段,私有的字段它无法访问,能访问从其它类继承来的公有方法.getDeclared ...

  6. js 原型

    1: function Person (name,age) { 2: this.name = name; 3: this.age = age; 4: } 5:   6: Person.prototyp ...

  7. py正则表达式

    1.元字符 . ^ $ * + ? {} [] \ | () --> [] :   - 常用来指定一个字符集:[abc], [a-z]  匹配任意一个字符 - 元字符在字符集中不起作用:[akm ...

  8. 关于url拼接传参数和利用view的字典传参数时,模板获取数据的方式问题

    url = "{% url 'dashboard:internship-theme-stat' %}?teacher_name="+teacher_name+"& ...

  9. oc语言--description方法和sel

    一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象方法) 使用NSLog和@%输出某个对象时, ...

  10. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...