【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 ...
随机推荐
- PostgreSQL处理xml数据初步
磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页[作者 高健@博客园 luckyjackgao@g ...
- Tarjan/2-SAT学习笔记
Tarjan/2-SAT Tags:图论 作业部落 评论地址 Tarjan 用来求割边或者割点,求点双联通分量或者边双联通分量 点双联通分量:两个点之间有两条点不相交的路径 边双联通分量:两个点之间有 ...
- 【python3】酷狗音乐及评论回复下载
新年快乐,上班第一天分享一个python源码,功能比较简单,就是实现酷狗音乐的音乐文件(包含付费音乐)和所有评论回复的下载. 以 米津玄師 - Lemon 为例, 以下为效果图: 1.根据关键词搜索指 ...
- 2_C语言中的数据类型 (六)浮点数
1.1 浮点float,double,long double类型 1.1.1 浮点常量,变量 Float在32位系统下是4个字节,double在32位系统下是8个字节 小 ...
- 联赛emacs配置
(custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you co ...
- JAVAWEB dbutils执行sql命令并遍历结果集时不能查到内容的原因
遍历结果集时只遍历bean对象才会只输出第一行那种内容(第一行是输出了UserEntity类实例化的对象),所以这里需要 re.getRepoTableName() 才能通过对象调用相对应的内容 这样 ...
- shell命令注意点
unset 不能删除readonly的变量 实例: #!/bin/bash name="lalala" readonly name unset name 执行结果: line5:u ...
- pycharm如何回到过去某个时间
在编写代码是,我们可能会写错代码,或者是误删某个文件,那么问题来了,如何回到过去的某个时间段,来弥补我们犯下的错呢? 1.如果是恢复删除的文件则右击之前文件所在的文件夹 2.右击文件夹的显示效果如图 ...
- CF刷题-Codeforces Round #481-F. Mentors
题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...
- 算法工程师进化-SQL
1 引言 SQL操作往往是程序员必备的技能,对于算法工程师而言,熟练掌握SQL操作则更为重要.本文以<SQL语句执行顺序>作为学习资料,总结SQL的理论部分. 2 SQL查询语句的执行顺序 ...