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 解题思路的更多相关文章

  1. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. [Leetcode] Backtracking回溯法解题思路

    碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...

随机推荐

  1. ASP.NET服务端基本控件介绍

    ASP.NET服务端基本控件介绍 大概分为三种控件: HTML控件,ASP.NET把HTML控件当成普通字符串渲染到浏览器端,不去检查正确性,无法在服务端进行处理ASP.NET服务端控件,经过ASP. ...

  2. 【转】Oracle job procedure 存储过程定时任务

    原文:Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相 ...

  3. React组件一

    <div id='test'></div> <script type='text/babel'> var Zu=React.createClass({ return ...

  4. Activity组件的UI实现

    Activity组件的UI实现需要与WindowManagerService服务和SurfaceFlinger服务进行交互 1. Activity组件在启动完成后,会通过一个类型为Session的Bi ...

  5. Core Animation

    position和anchorPoint的区别  -整理自苹果官方文档- Layers使用两种坐标系: 1. point-based  :1)当需要定义layer在屏幕中或是距另一个layer的位置时 ...

  6. 如何利用SecureCRT连接Ubuntu12.0.4

    环境描述:虚拟机网络选择NAT连接方式,Ubuntu的版本是Ubuntu12.0.4 1. 先做一个测试,假设现在系统还没有装ssh,用secureCRT连接Ubuntu是出现下面的界面. 实际上,这 ...

  7. POJ 1716 Integer Intervals 差分约束

    题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...

  8. nutch http file 截断问题

    问题: 列表页预计抽取 355+6 但实际只抽取到220条链接. 原因是nutch对http下载的内容的长度进行了限制. 解决方案:这里将这个属性扩大10倍. vim conf/nutch-defal ...

  9. Li Fei-fei写给她学生的一封信,如何做好研究以及写好PAPER

    Li Fei-fei写给她学生的一封信,如何做好研究以及写好PAPER 在微博上看到的,读完还是有些收获的,首先是端正做research的态度. 我是从这里看到的:http://www.vjianke ...

  10. 小波变换 C++ opencv 实现

    小波变换 C++ opencv 实现 小波简介: http://www.blogbus.com/shijuanfeng-logs/221293135.html 源码:   /// 小波变换 Mat W ...