Question:

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.

Tips:

给定一个无序的链表,将链表分成两部分,前半部分的val全部小于x;后半部分大于等于x。

前后两部分的结点,相对位置不变,即node1 与node2 均大于x 那么不管node1与node2的val谁大,最终顺序仍未node1->node2.

思路:

初始化两个头结点small 与big,然后开始遍历链表,当head.val>=x ,就将head接到big.next上。否则接到small.next上。

head走到末尾,将small的最后一个结点与big的第一个结点连接,返回small的第一个结点即可。

代码:

public ListNode partition(ListNode head, int x) {
if (head == null)
return head;
ListNode big = new ListNode(-1);
ListNode small = new ListNode(-1);
ListNode temp = big;// 记录big的第一个结点位置
ListNode result = small;// 记录small的第一个结点位置
while (head != null) {
if (head.val >= x) {
big.next = head;
big = big.next; } else {
small.next = head;
small = small.next;
}
head = head.next;
}
//small的最后一个结点与big的第一个结点连接
small.next = temp.next;
big.next = null;
return result.next;
}

测试代码:

public static void main(String[] args) {
L86PartitionList l86 = new L86PartitionList();
ListNode head1 = new ListNode(1);
ListNode head2 = new ListNode(4);
ListNode head3 = new ListNode(2);
ListNode head4 = new ListNode(5);
ListNode head5 = new ListNode(3);
ListNode head6 = new ListNode(6);
ListNode head7 = null;
head1.next = head2;
head2.next = head3;
head3.next = head4;
head4.next = head5;
head5.next = head6;
head6.next = head7;
ListNode head = l86.partition(head1, 30);
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}

【Leetcode】86. Partition List的更多相关文章

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

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

  2. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  3. 【一天一道LeetCode】#86. Partition List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】86. 分隔链表

    86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...

  5. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】763. Partition Labels 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

  7. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  8. 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  9. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

随机推荐

  1. [2016北京集训试题6]mushroom-[bitset]

    Description Solution bitset是个好东西啊..强行压位什么的真是够orz. 由于所有的蘑菇上房间的长相是一样的,我们针对每个房间,算出它到根节点的bitset和以它为根的子树的 ...

  2. mfc 私有成员

    知识点 类的私有成员private 访问类的私有成员 内联函数inline 一.类的私有成员 用private定义的(变量)或者(函数)只能在本类中使用其他类不能中不能调用: 用public定义的(变 ...

  3. tkinter的GUI设计:界面与逻辑分离(二)-- 菜单栏

    由于要用到文件对话框和消息对话框,所以先给出下面的列表. py2 与 py3 中 tkinter 的变化: Tkinter → tkinter tkMessageBox → tkinter.messa ...

  4. 4 CRM-权限管理rbac、github代码

    1.引入权限组件rbac 1.settings配置app.中间件 INSTALLED_APPS = [ ... ... 'crm.apps.CrmConfig', "stark.apps.S ...

  5. 2 timeit模块,python中数据结构

    1.timeit模块:代码事件测量模块 timeit模块可以用来测试一小段Python代码的执行速度. class timeit.Timer(stmt='pass', setup='pass', ti ...

  6. springboot之websocket,STOMP协议

    一.WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 ...

  7. idea maven javaweb项目迁移时的maven和版本报错问题解决(可解决同类错误)

    项目中代码红线报版本不支持xx语法,只需要将java版本设置为当前机器使用的java版本即可 这里我使用的是idea自带的maven,如果是自己安装的maven需要在 home directory 处 ...

  8. springmvc框架开发中解决产生的乱码情况

    一:解决post请求方式产生的乱码情况 示例代码如下: <!-- 解决post乱码 --> <filter> <filter-name>CharacterEncod ...

  9. 安装php xdebug调试工具及性能分析工具webgrind for windows

    安装php xdebug调试工具及性能分析工具webgrind for windows 第一步:查看php版本等信息 phpinfo(); 上面是 x86 NTS VC14 第二步: 下载xdebug ...

  10. 2019网易笔试题C++--丰收

    题目描述 又到了丰收的季节,恰好小易去牛牛的果园里游玩. 牛牛常说他多整个果园的每个地方都了如指掌,小易不太相信,所以他想考考牛牛. 在果园里有N堆苹果,每堆苹果的数量为ai,小易希望知道从左往右数第 ...