【Leetcode】86. Partition List
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的更多相关文章
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【一天一道LeetCode】#86. Partition List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】86. 分隔链表
86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】763. Partition Labels 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- 【LeetCode】698. Partition to K Equal Sum Subsets 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
随机推荐
- 2017-2018-1 20155210 《信息安全系统设计基础》 实现mypwd
2017-2018-1 20155210 <信息安全系统设计基础> 实现mypwd 作业要求: 1.学习pwd命令 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 ...
- vue动态修改title
1.项目中,cmd下 ,运行:cnpm install vue-wechat-title --save 2.在 main.js 中,设置: import VueWechatTitle from 'vu ...
- bzoj 前100题计划
bzoj前100题计划 xz布置的巨大的坑.. 有空填题解... 1002 轮状病毒 用python手动matrixtree打表. #include<bits/stdc++.h> #def ...
- JavaScript快速查找节点
我们在实际的开发中,经常要获取页面中某个html元素,动态更新元素的样式.内容属性等. 我们已经知道在JavaScript中提供下面的方法获取子.父.兄节点的方法: 常规 通过父节点获取子节点: pa ...
- Linux(centos7)之更换安装python3(二)
Linux不比window好安装python,折腾了好久,终于成功安装上了python,window上一个安装包完事,可惜Linux上python版本太低不好使,还要更换为3版本,百度了好久,教程上总 ...
- zabbix-agent报错:zabbix_agentd [5922]: cannot open log: cannot create semaphore set: [28] No space left on device
起因: 新添加完自定义监控项后重启zabbix-agent时失败,查看/var/log/zabbix/zabbix-agentd.log 发现系统提示zabbix共享内存报错 zabbix_agent ...
- 人工智能AI芯片与Maker创意接轨(下)
继「人工智能AI芯片与Maker创意接轨」的(上)篇中,认识了人工智能.深度学习,以及深度学习技术的应用,以及(中)篇对市面上AI芯片的类型及解决方案现况做了完整剖析后,系列文到了最后一篇,将带领各位 ...
- 多表查询sql语句
多表查询sql语句 1 --解锁SCOTT用户 2 alter user scott account unlock 3 --检索指定的列 4 select job,ename,empno from e ...
- GearCase UI - 自己构建一套基于 Vue 的简易开源组件库
最近 1 ~ 2 月除了开发小程序之外,还一直在继续深入的学习 Vuejs.利用零碎.闲暇的时间整合了一套基于 Vue 的 UI 组件库.命名为 GearCase UI,意为齿轮盒.现在把该项目进行开 ...
- Go入门指南
第一部分:学习 Go 语言 第1章:Go 语言的起源,发展与普及 1.1 起源与发展 1.2 语言的主要特性与发展的环境和影响因素 第2章:安装与运行环境 2.1 平台与架构 2.2 Go 环境变量 ...