码迷,mamicode.com
首页 > 其他好文 > 详细

86. Partition List

时间:2016-05-15 09:37:26      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 86. Partition List 
     * 11.26 by Mingyang
     * 没有什么诀窍,就是分成两个list,一个装小的一个装大的
     * 只是注意small.next = head;赋值要准确,不能够错误,不能small=head
     */
    public ListNode partition(ListNode head, int x) {
        if (head == null || head.next == null)
            return head;
        ListNode small = new ListNode(-1);
        ListNode newsmallhead = small;
        ListNode big = new ListNode(-1);
        ListNode newbighead = big;
        while (head != null) {
            if (head.val < x) {
                small.next = head;
                small = small.next;
            } else {
                big.next = head;
                big = big.next;
            }
            head = head.next;
        }
        big.next = null;
        small.next = newbighead.next;
        return newsmallhead.next;
    }

 

86. Partition List

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5494597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!