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的值分成两部分。详细思路和代码例如以下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
/**
* 思路是将list按X分成两段
* 小于的连接p
* 大于的连接q
* 最后合并p和q就可以
*/
ListNode p = new ListNode(0);
ListNode pHead = p;
ListNode q = new ListNode(0);
ListNode qHead = q;
//遍历
while(head != null){
if(head.val < x){//<x成一组
p.next = head;
p = p.next;
}else{//>=x成一组
q.next = head;
q = q.next;
}
head = head.next;
}
p.next = qHead.next;
q.next = null;//斩断后面的连接
return pHead.next;
}
}

leetCode 86.Partition List(分区链表) 解题思路和方法的更多相关文章

  1. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

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

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

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

  5. [LeetCode]86. Partition List分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

  6. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  8. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  9. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

随机推荐

  1. java短信接口调用

    java短信接口调用 之前一直在一个传统的单位上班好多听容易的技术都没接触过,即使有时候想搞一搞类似于支付宝支付,短信接口调用,微信公众号,小程序之类等功能,一直有心无力终于跳槽了,估计是氛围的原因吧 ...

  2. php获取当前域名、主机、URL、端口、参数、网址、路径、代理等【转】

    <?php //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br />"; //获取网页地址 echo $_SERVER['PH ...

  3. 通过openURL的方式启动其它App

    假设有两个App,项目名分别是SampleA和SampleB,需要在SampleA里点击一个Button来启动SampleB,并传递一个字符串.具体实现步骤如下: 1. 在SampleB的info.p ...

  4. Tornado(二)

    跨站请求伪造CSRF 开启xsrf(就是叫法不一样和csrf一样),'xsrf_cookies':True settings = { 'template_path':'template', 'stat ...

  5. BZOJ [JSOI2008]星球大战starwar

    正着显然不可做,我们采取反向并查集,将删点改为加点,每次贪心的认为加了一个联通块,一旦不符就减一. #include<bits/stdc++.h> using namespace std; ...

  6. 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】

    题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...

  7. HDU 6085 Rikka with Candies(bitset)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6085 [题目大意] 给出一个数组a一个数组b,以及询问数组c, 问对于每个c有多少对a%b=c,答 ...

  8. CXF和Axis2开发webservice也是可以添加asmx等后缀

    在当家互联网时代, 手机APP所需要的后台服务接口经常会变化, 如果前期没有设计好, 把它们的请求地址配置在比较稳定不会经常修改的地址(例如专门一个后台服务用于获取所有最新的数据服务地址)这样不会因为 ...

  9. bzoj 2428: [HAOI2006]均分数据 随机化

    2428: [HAOI2006]均分数据 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  10. When to use static method in a java class

    First , please understand its feature : * no need to instantiate a instance, i.e. simply you can jus ...