[leetcode]86. Partition List划分链表
2024-10-15 22:07:38
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.
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
题意:
给定一个链表和一个值,把小于等于和大于该值的部分分别放到链表的一前一后去。
思路:
先分为两个链表
然后合并
代码:
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode leftDummy = new ListNode(-1);
ListNode rightDummy = new ListNode (-1);
ListNode left_cur = leftDummy;
ListNode right_cur = rightDummy;
ListNode cur = head; while( cur != null){
if(cur.val < x){
left_cur.next = cur;
left_cur = cur;
}else{
right_cur.next = cur;
right_cur = cur;
}
cur = cur.next;
}
left_cur.next = rightDummy.next;
right_cur.next = null;
return leftDummy.next;
}
最新文章
- 如何用Unity创建一个的简单的HoloLens 3D程序
- 如何配置Eclipse+Tomcat 开发环境【转】
- [Effective Java]第六章 枚举和注解
- static inline
- 验证码在IE中不刷新
- OnTouchListener事件监听实现方式之GestureDetector
- 【原创】MySql 数据库导入导出(备份)
- CURL与PHP-CLI的应用【CURL篇】
- HDU-5347 MZL&#39;s chemistry
- js事件3
- mysql 事务控制
- COJ 1102 - You Can Say 11 题解
- C++通过Callback向C#传递数据,注意问题
- 我的第一个python web开发框架(5)——开发前准备工作(了解编码前需要知道的一些常识)
- 349B - Color the Fence
- P3954 成绩(noip2017普及组)
- 在Mac 系统上使用MAMP搭建PHP开发环境
- (转)java术语(PO/POJO/VO/BO/DAO/DTO)
- Lucene全文检索入门使用
- mysql5.6以上版本: timestamp current_timestamp报1064/1067错误