Leetcode 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
.
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的更多相关文章
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [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] Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [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 ...
- [leetcode]Partition List @ Python
原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- LeetCode - Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- 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 Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- Extjs ComboBox 动态选中第一项
有时候我们希望通过Store加载过来的数据,ComboBoxItem能够选择第一条数据作为默认数据,我们可以这么操作: var storeinfo = Ext.create('Ext.data.Sto ...
- 网站性能测试工具--MS Web Application Stress Tool
MS Web Applicaion Stress Tool 是一款网页测试的性能工具,具体的使用可以参考下面这篇博客文章 http://cuisuqiang.iteye.com/blog/193640 ...
- SQLAlchemy ORM高级查询之过滤,排序
order_by,filter的语法. 用久了才会熟悉. Session = sessionmaker(bind=engine) session = Session() print(session.q ...
- [LeetCode] Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- jQuery easyUI datagrid 增加求和统计行 分类: JavaScript 2015-01-14 17:46 2178人阅读 评论(0) 收藏
在datagrid的onLoadSuccess事件增加代码处理. <style type="text/css"> .subtotal { font-weight: bo ...
- 关于socket——SO_SNDBUF and SO_RECVBUF
转自:http://blog.csdn.net/wf1982/article/details/38871521 参见 http://stackoverflow.com/questions/425741 ...
- 【c++】必须在类初始化列表中初始化的几种情况
转自:http://www.cnblogs.com/kaituorensheng/p/3477630.html 1. 类成员为const类型 2. 类成员为引用类型 #include <iost ...
- MathType 常用快捷键
MathType 数学公式编辑器是广大理科生电脑上必不可少的软件!然而在大量公式时,不会巧妙使用快捷键真的是心累身累.巧妙使用MathType数学公式编辑器可加快数学符号的录入速度和效率,这将节约大量 ...
- jQuery Mobile Datepicker 使用
插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...
- UED
User Experience Design(用户体验设计),简称UED.UED是进行产品策划的主力之一,他们用自己的知识.经验.设计能力拿出设计方案. UED不只是互联网专家,还是行业专家.能够用自 ...