Tuesday, August 15th

 Linked List Partitioning

Given a linked list and a value x, partition the list so that values less than x occur before values greater than or equal to x, while maintaining the original ordering

From by Sandstein via Wikipedia, distributed under the Creative Commons Attribution-Share Alike 4.0 International license. No changes made

A fairly straight-forward linked list problem

First, check if the list is empty

    if(head==NULL) return NULL;

Maintain 2 index pointers, 1 for the less than list and another for the greater than list.  Also maintain 2 constant pointers that point to the head of the lesser than list and the greater than list

    ListNode* lhead=NULL, *ghead=NULL;
    ListNode* lindex=NULL, *gindex=NULL;

 Traverse the linked list

    while(head!=NULL) {
        ...
        head=head->next;
    }

for each traversed node, check if it's less than or greater that or equal to the target value

    if(head->val >= x) {
        ...
    } else {
        ...
    }

If the value is greater than or equal to x, check if the greater than or equal to head node has been set.  If so, set the greater-than-or-equal to head node, and also set the g-index node.  Otherwise, set the g-index node and traverse the node

    if(ghead==NULL) {
        ghead=head;
        gindex=head;
    } else {
        gindex->next=head;
        gindex=gindex->next;
    }

Perform the equivalent operations for the less-than-x nodes

   if(lhead==NULL) {
        lhead=head;
        lindex=head;
    } else {
        lindex->next=head;
        lindex=lindex->next;
    }

Perform a final few operations: set the last node to empty, if the first partition is empty return the second partition, and finally, set the first partition end to the second partition

    if(gindex!=NULL) gindex->next=NULL;
    if(lhead==NULL) return ghead;
    lindex->next=ghead;

return the head of the first partition

    return lhead;

Straight-forward linked-list problem.  Engineering.

Full code below

    ListNode* partition(ListNode* head, int x) {
        if(head==NULL) return NULL;
       
        ListNode* lhead=NULL, *ghead=NULL;
        ListNode* lindex=NULL, *gindex=NULL;

       while(head!=NULL) {
           if(head->val >= x) {
               if(ghead==NULL) {
                   ghead=head;
                   gindex=head;
               } else {
                   gindex->next=head;
                   gindex=gindex->next;
               }        
           } else {
               if(lhead==NULL) {
                    lhead=head;
                    lindex=head;
               } else {
                   lindex->next=head;
                   lindex=lindex->next;
               }
           }
           head=head->next;
       }
        if(gindex!=NULL) gindex->next=NULL;
        if(lhead==NULL) return ghead;
        lindex->next=ghead;

        return lhead;
    }

Comments

Popular posts from this blog

Wednesday, September 20th

Monday, September 11th

Tuesday, September 19th