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

Partition List; Linked List; Pointer;

时间:2016-01-24 09:13:58      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

At the first sight of the problem, I misunderstood it as sort the former part of the list and then keep the original order of those nodes in the larger or equal to target value part. Thus, I sort the first part and get a wrong result. 

Code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode cur = head;
        ListNode head1 = null, tail1 = null;
        ListNode head2 = null, tail2 = null;
        while(cur != null){
            ListNode nex = cur.next;
            ListNode node = cur;
            if(node.val < x){
                ListNode cur1 = head1; 
                ListNode prev1 = null;
                while(cur1 != null && cur1.val < node.val) {
                    prev1 = cur1;
                    cur1 = cur1.next;
                }
                if(prev1 != null) prev1.next = node;
                else head1 = node;
                cur.next = cur1;
                if(cur1 == null) tail1 = node;
            }
            else{
                ListNode cur2 = tail2;
                if(cur2 != null){
                    tail2.next = node;
                    node.next = null;
                    tail2 = node;
                }
                else{
                    head2 = node;
                    tail2 = node;
                    node.next = null;
                }
            }
            cur = nex;
        }
        if(tail1 != null) tail1.next = head2;
        if(head1 == null) return head2;
        return head1;
    }
}

 

After a littile modification, we can the the right one:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode cur = head;
        ListNode head1 = null, tail1 = null;
        ListNode head2 = null, tail2 = null;
        while(cur != null){
            ListNode nex = cur.next;
            ListNode node = cur;
            if(node.val < x){
                ListNode cur1 = tail1;
                if(cur1 != null){
                    tail1.next = node;
                    node.next = null;
                    tail1 = node;
                }
                else{
                    head1 = node;
                    tail1 = node;
                    node.next = null;
                }
            }
            else{
                ListNode cur2 = tail2;
                if(cur2 != null){
                    tail2.next = node;
                    node.next = null;
                    tail2 = node;
                }
                else{
                    head2 = node;
                    tail2 = node;
                    node.next = null;
                }
            }
            cur = nex;
        }
        if(tail1 != null) tail1.next = head2;
        if(head1 == null) return head2;
        return head1;
    }
}

 

Partition List; Linked List; Pointer;

标签:

原文地址:http://www.cnblogs.com/5683yue/p/5154537.html

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