Partition List; Linked List; Pointer;

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Partition List; Linked List; Pointer;相关的知识,希望对你有一定的参考价值。

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;的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 86. Partition List_Medium tag: Linked List

leetcode-86-partition list

206. Reverse Linked List

234. Palindrome Linked List

Reverse Linked List | & ||

LeetCode 206 Reverse a singly linked list.