LeetCode2.2.3 @ Partition List 链表划分 D3F3

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,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

【3.对于涉及head节点的操作,不如ListNode *dummy = new ListNode(0); dummy->next = head;;使针对head节点的处理与其他节点一致。最后返回dummy->next作为头指针即可。

思路:制造两个dummy node,消除头结点的额外操作。

public class Solution {
    public ListNode partition(ListNode head, int x) {
        if(head==null)
            return null;
        ListNode dummy1=new ListNode(0);//dummy doesnt connect to org list,no need "dummy1.next=head"
        ListNode dummy2=new ListNode(0);
        ListNode cur=head;
        ListNode cur1=dummy1;
        ListNode cur2=dummy2;
        while(cur!=null){
            if(cur.val<x){
                cur1.next=cur;
                cur1=cur;
            }
            else{
                cur2.next=cur;
                cur2=cur;
            }
            cur=cur.next;
        }//end while
        cur1.next=dummy2.next;
        cur2.next=null;//w/o it, case {2,1},2 will time out cuz cycle list
        return dummy1.next;
    }
}

双指针解法没看懂。。

http://blog.csdn.net/linhuanmars/article/details/24446871


附:之前做CC150时候的原始方法,定义两组头尾引用。缺点在于head是否为空要单独划分出情况。

public class Solution {
    public ListNode partition(ListNode head, int x) {
        if(head==null)
            return null;
        ListNode head1=null;
        ListNode tail1=null;
        ListNode head2=null;
        ListNode tail2=null;
        ListNode cur=head;
        
        while(cur!=null){
            if(cur.val<x){
                if(head1==null){
                    head1=cur;
                    tail1=cur;
                }
                else{
                    tail1.next=cur;
                    tail1=cur;
                }
            }
            else{
                if(head2==null){
                    head2=cur;
                    tail2=cur;
                }
                else{
                    tail2.next=cur;
                    tail2=cur;
                }
            }
            cur=cur.next;
        }
        
        if(head1==null)
            return head2;
        if(head2==null)
            return head1;
        if(head1!=null && head2!=null){
            tail1.next=head2;
            tail2.next=null;//【注意】
        }
        return head1;
    }
}
【注意】没有加这句话,造成了循环链表,{2,1},2的testcase超时。另外注意head1,head2为null的情况。

所以涉及头指针的操作,还是定义dummy node好。


【后记】待验证的真理---如果init值为第一个Node,那么while(curr!=null),;如果init值为第一个Node的preNode,那么while(pre.next!=null)。。。想法come up于遍历全表。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值