[LeetCode] 86. 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
.
问题:给定一个列表和整数 x,将列表中所有小于 x 的元素移到大于或等于 x 的元素前面。要求,移动后两部分的内部的元素相对位置和原来的保存一致。
一看到题目,需要将一列数分为小于 x 和 大于或等于 x 两部分,首先想到的是用两个指针从两段向中间扫来求解。但是题目是列表,不能从后往前扫,并且移动后的相对位置要保存和之前一致,则不能用这种方法。
第二个思路是用数组存储新的列表顺序,然后在数组中建立 元素间的指针关系。这个思路比较简单,也提交通过了。
ListNode* partition(ListNode* head, int x) { if (head == NULL){
return NULL;
} vector<ListNode*> arr; ListNode* tmp = head;
while (tmp != NULL) { if (tmp->val < x) {
arr.push_back(tmp);
} tmp = tmp->next;
} tmp = head;
while (tmp != NULL) { if (x <= tmp->val) {
arr.push_back(tmp);
} tmp = tmp->next;
} for (int i = ; i < arr.size()-; i++) {
arr[i]->next = arr[i+];
}
arr[arr.size()-]->next = NULL; head = arr[]; return head; }
[LeetCode] 86. Partition List 解题思路的更多相关文章
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 134. Gas Station 解题思路
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] 16. 3Sum Closest 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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 86. Partition List 划分链表 C++
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] Backtracking回溯法解题思路
碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...
随机推荐
- EntityClient 介绍
System.Data.EntityClient 命名空间是 实体框架的 .NET Framework 数据提供程序.EntityClient 提供程序使用存储特定的 ADO.NET 数据提供程序类和 ...
- jquery 多级无限分类
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 获取DOM的真实节点
<script type="text/babel"> var Myelement=React.createClass({ handleClick:function(){ ...
- JavaScript原型链demo
function Person(name){ this.name = name; } Person.prototype = { say: function(){ alert('hi'); }, say ...
- php文件上传大小限制设置
配置选项说明: upload_max_filesize 所上传的文件的最大大小. post_max_size 设定 POST 数据所允许的最大大小. memory_limit 设定了一个脚本所能够申请 ...
- linux中轻松使用backspace和上下按键
linux下使用sqlplus真是太费事儿了,backspace和上下键均出现乱码.在公司服务器上操作时,只能先用delete代替backspace,上下键就直接重新输入代替了,很费时间.今天在自己机 ...
- rabbitmq 测试
--*-- import pika import datetime connection = pika.BlockingConnection(pika.ConnectionParameters( 'l ...
- django开发总结:
一,关于setting目录中的“DEBUG” DEBUG=False 把DEBUG从True改成False后就会出现(必需指定404和500错语页面,如上图的目录结构)找不到页面的错误.原因是DEBU ...
- const char*, char const*, char*const的区别
http://www.cnblogs.com/aduck/articles/2244884.html
- Service知识点总结
转载请注明出处:http://blog.csdn.net/krislight/article Service可以看作一个后台服务,但并非是开启另外的线程,Service还是在主线程中运行.所以需避免耗 ...