简体   繁体   中英

Partitioning a linked list around a integer value

I'm trying to write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x need to be after the elements less than x. However, the partition element x can appear anywhere in the "right partition".

Input 3->5->8->5->10->2->1 [partition =5] Output 3->1->2->10->5->5->8

The Node class is defined as below.

class Node{
    Node next = null;
    int data;
    public Node(int d){
        data =d;
    }
    public Node() {
        // TODO Auto-generated constructor stub
    }
    void appendToTail(int d){
        Node end = new Node(d);
        Node n = this;
        while(n.next!=null){
            n = n.next;
        }
        n.next = end;
    }

Below is a half working code that I came up with.

static void Partition(Node n,int numb){
    Node tail = n;
    while(tail.next != null){
        tail = tail.next;
    }

Node current = n;

Node tailCurrent = tail;
Node prev = null;
while(current!= tailCurrent){
    if(current.data<numb){

        prev = current;
        System.out.println(prev.data);
    }
    else{
        prev.next = current.next;
        tail.next = current;
        tail = current;

    }
    current =current.next;
}


tail.next = null;


}

It works fine for the cases where the head node has an integer less than the partitioning number, however the error occurs when the head node has an integer greater than or equal to partitioning number. I believe this is because in the else statement in my code, prev.next throws a null pointer exception because it never goes through the if case to have a non-null value. Can anyone suggest a way to fix this? Thank you.

When there is no previous node to update, you can simply do nothing.

So change this:

prev.next = current.next;

to this:

if (prev != null) {
    prev.next = current.next;
}

There is another problem: the root of the list may change during partitioning, and there is no way for the calling code to get access to the new root. It can be fixed by making the Partition method keep track of where the current root is and return it in the end.

static Node Partition(Node n, int numb) {
    Node tail = n;
    while(tail.next != null) {
        tail = tail.next;
    }
    Node current = n;
    Node root = n;
    Node tailCurrent = tail;
    Node prev = null;
    while(current != tailCurrent) {
        if(current.data < numb) {
            prev = current;
        } else {
            if (prev != null) {
                prev.next = current.next;
            } 
            if (root == current) {
                root = current.next;
            }
            tail.next = current;
            tail = current;
        }
        current = current.next;
    }
    tail.next = null;
    return root;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM