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;
}

最新文章

  1. 如何用Unity创建一个的简单的HoloLens 3D程序
  2. 如何配置Eclipse+Tomcat 开发环境【转】
  3. [Effective Java]第六章 枚举和注解
  4. static inline
  5. 验证码在IE中不刷新
  6. OnTouchListener事件监听实现方式之GestureDetector
  7. 【原创】MySql 数据库导入导出(备份)
  8. CURL与PHP-CLI的应用【CURL篇】
  9. HDU-5347 MZL&#39;s chemistry
  10. js事件3
  11. mysql 事务控制
  12. COJ 1102 - You Can Say 11 题解
  13. C++通过Callback向C#传递数据,注意问题
  14. 我的第一个python web开发框架(5)——开发前准备工作(了解编码前需要知道的一些常识)
  15. 349B - Color the Fence
  16. P3954 成绩(noip2017普及组)
  17. 在Mac 系统上使用MAMP搭建PHP开发环境
  18. (转)java术语(PO/POJO/VO/BO/DAO/DTO)
  19. Lucene全文检索入门使用
  20. mysql5.6以上版本: timestamp current_timestamp报1064/1067错误

热门文章

  1. groovy学习知识
  2. Windows下struct和union字节对齐设置以及大小的确定(一 简介和结构体大小的确定)
  3. 关于动态内存malloc和realloc
  4. 初识nginx反向代理和缓存机制
  5. ByteToByte64String、Base64StringToBytes
  6. [UE4]从零开始构建VR角色
  7. centos7将可执行程序做成服务
  8. 浅谈Cookie与Session技术
  9. Maven CXF wsdl2Java String生成JAXBElement&lt;Xxx&gt; 解决方法
  10. 为何要使用ViewModel