[Linked List]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.
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的更多相关文章
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 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 ...
- 【leetcode】Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- 【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 ...
- Leetcode Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 46. Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- [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 ...
- [LeetCode]题解(python):086 - Partition List
题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...
- 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- asp.net mvc 访问.html文件
把html页面放在除Views文件夹外的任意文件夹,如Htmls--123.html,url直接访问便可,如 http://yourname/htmls/123.html 可以在路由表中排出不需要路由 ...
- SQL Server一些常见却不太记得住的命令
一.数据库大小查询 1. exec sp_spaceused '表名' --(SQL统计数据,大量事务操作后可能不准)2. exec sp_spaceused '表名', true ...
- PHP--变量部分知识点
PHP全局变量 PHP全局变量作用域不同与C,在函数内部不可以使用全局变量,要在函数内部使用全局变量需要,global $var或者使用超全局变量数组$GLOBALS['var']. 静态变量 PHP ...
- 【solr专题之一】Solr快速入门
一.Solr学习相关资料 1.官方材料 (1)快速入门:http://lucene.apache.org/solr/4_9_0/tutorial.html,以自带的example项目快速介绍发Solr ...
- 关于反射中获取Fields,method,Construts简单介绍
* getFields()与getDeclaredFields()区别:getFields()只能访问类中声明为公有的字段,私有的字段它无法访问,能访问从其它类继承来的公有方法.getDeclared ...
- js 原型
1: function Person (name,age) { 2: this.name = name; 3: this.age = age; 4: } 5: 6: Person.prototyp ...
- py正则表达式
1.元字符 . ^ $ * + ? {} [] \ | () --> [] : - 常用来指定一个字符集:[abc], [a-z] 匹配任意一个字符 - 元字符在字符集中不起作用:[akm ...
- 关于url拼接传参数和利用view的字典传参数时,模板获取数据的方式问题
url = "{% url 'dashboard:internship-theme-stat' %}?teacher_name="+teacher_name+"& ...
- oc语言--description方法和sel
一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象方法) 使用NSLog和@%输出某个对象时, ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...