[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)的概率明显增大 ...
随机推荐
- JS进制转换,浮点数相加,数字判断
document.write("整数转换函数:parseInt(数据,底数)<br>"); document.write("10101=>" ...
- 【转载】db blocks gets & consistent gets
LOGIC IO(逻辑读次数)= db block gets + consistent gets consistent get : 在一致读模式下所读的快数,包括从回滚段读的快数. db block ...
- STM32的GPIO
一.I/O端口位的基本结构 二.端口位配置表 参考:STM32芯片参考手册
- bootstrap table 服务器分页
1.封装MODEL using System;using System.Collections.Generic;using System.Linq;using System.Text;using Sy ...
- 12306 订票助手 C# 版
闲着没事,也用C#写了一个12306的订票助手,虽然可能会有些BUG但是也能正常使用了下载地址:http://www.fishlee.net/soft/12306_helper/ 查票窗口,可以查询余 ...
- Jar包可执行??
第一次听说,jvm加载包,必须rwx么?
- 【BZOJ 1096】 [ZJOI2007]仓库建设 (斜率优化)
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3940 Solved: 1736 Description ...
- eclipse+maven搭建cxf webservice 完整例子
开发环境是eclipse , maven. 在开发java webservice时,有两个比较流行的框架:axis2和cxf.cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在t ...
- 李洪强漫谈iOS开发[C语言-025]-赋值运算符案例
- MySQL查询原理及其慢查询优化案例分享(转)
MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更 好的使用它,已经成为开发工程师的必修课,我们经常会从职 ...