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. Python开发基础-Day31 Event对象、队列和多进程基础

    Event对象 用于线程间通信,即程序中的其一个线程需要通过判断某个线程的状态来确定自己下一步的操作,就用到了event对象 event对象默认为假(Flase),即遇到event对象在等待就阻塞线程 ...

  2. AtCoder Regular Contest 81

    链接 C.Make a Rectangle 给出一堆木棍的长度 从中选4根,询问在能围成矩形的情况下,矩形的最大面积 开个map统计一下就行 分正方形和矩形分别统计即可 复杂度$O(n \log n) ...

  3. 【tarjan】BZOJ2140-稳定婚姻

    又名NTR的故事 [题目大意] n对夫妻Bi和Gi.若某男Bi与某女Gj曾经交往过,他们有私奔的可能性.不妨设Bi和Gj旧情复燃,进而Bj会联系上了他的初恋情人Gk,以此递推.若在Bi和Gi离婚的前提 ...

  4. bzoj 1030

    dp[i][j] 表示,在AC自动机中,从根节点开始,走了i条边,并且经过的点不包含危险节点,走到了j节点的路径数. 收获: 1.正难则反 2.一个字符串不包含给定pattern中的任何一个,则该字符 ...

  5. Mac下配置Idea的Maven

    环境版本: Mac OS: 10.13.4 JDK: 1.8 Idea: 2018.3 Maven: 3.6.0 Maven 相关配置: Maven 下载: http://maven.apache.o ...

  6. Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题

    C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...

  7. 解决新版本webpack vue-cli生成文件没有dev.server.js问题

    新版本webpack生成的dev.server.js 在webpack.dev.conf.js中 webpack.dev.conf.js const axios = require('axios') ...

  8. js中常用日期时间转换

    常用日期时间处理插件:1. timeago.js处理几分钟之前    2. day.js    3. moment.js 注意: 1. 此处的标准时间格式为  2018-03-23 13:35:47 ...

  9. Spark编程指南V1.4.0(翻译)

    Spark编程指南V1.4.0 ·        简单介绍 ·        接入Spark ·        Spark初始化 ·        使用Shell ·        在集群上部署代码 ...

  10. 关于ReactiveCocoa的实际用法

    监听UITextView控件 text属性 @weakify(self); [self.textViewAdvice.rac_textSignal subscribeNext:^(NSString * ...