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,
Given1->4->3->2->5->2and x = 3,
return1->2->2->4->3->5.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
           
        if(head==null)
            return null;
        ListNode  smallHead=new ListNode(0);
        ListNode  bigHead=new ListNode(0);
        ListNode small=smallHead;
        ListNode big=bigHead;
        ListNode p=head;
        while(p!=null){
            if(p.val>=x){
                big.next=p;
                big=big.next;
            }else{
                small.next=p;
                small=small.next;
            }
            p=p.next;
        }
        small.next=bigHead.next;
                big.next=null;//必须把后面断掉,因为原来的节点还指着后面。我这里犯错误了,没有断掉,导致报错
        return smallHead.next;
        
    }
    
}

最新文章

  1. LeetCode-1TwoSum(C#)
  2. shell 脚本技巧
  3. C#中==与Equals方法的区别
  4. 第7章 jQuery插件的使用和写法
  5. DOM解析
  6. Java-Stack
  7. Codeforces Round #293 (Div. 2)
  8. Appium 切换上下文环境
  9. [转]PO和VO、关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )
  10. 从一次面试经历谈PHP的普通传值与引用传值以及unset
  11. MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
  12. cookie特殊字符在游览器被转义
  13. .net core 2.0 数据访问-迁移
  14. h5调用手机相册摄像头以及文件夹
  15. Ubuntu 守护进程
  16. 实用的php购物车程序
  17. PHP注释的艺术——phpDoc规范
  18. 2018-2019 ACM-ICPC 焦作赛区 部分题解
  19. Oracle之标示符无效
  20. Docker 三剑客

热门文章

  1. HDU 3078 (LCA+树链第K大)
  2. C++11 feature: move constructor
  3. Code[VS] 2370 LCA 题解
  4. 定义 iOS 方法名等不错的规范
  5. SpringMVC+Thymeleaf如何处理URL中的动态查询参数
  6. window下 配置gitlab ssh非端口22端口
  7. HighchartsJS创建点状带标识的图表实例
  8. EasyUI组件(窗口组件)
  9. while:1.兔子生兔子问题 2.打印菱形 3.求100以内质数的和4.洗发水15元一瓶,牙膏5元一支,香皂2元一块,150元刚好花完
  10. Winform程序以Icon的形式显示在任务栏右下角