Thursday, September 7th

 Reverse part of a linked list

Given a linked list and 2 positions left and right, reverse the linked list in between the 2 positions

The football play portrayed above is an end around, sometimes a precursor to a reverse

I came up with a couple of solutions that solved the problem by swapping values between nodes the being reversed, but as some problem commenters pointed out, this isn't exactly the same as reversing part of the list.  

Moreover, one could easily imagine being asked this question in an interview, and upon further clarification, begin required to reverse the whole list, not just values.  

Reversing the actual list nodes takes a little more thought, so we'll present that here.

We start the function by checking for an empty head

ListNode* reverseBetween(ListNode* head, int left, int right) {
if(head==NULL) return NULL;
        ...
    }

Next we declare 2 pointers, current and previous.  Then we iterate over the list left-1 times, updating the previous pointer to current, and the current to the next node.  The reason we iterate only left-1 times is because the list is indexed at 1

ListNode *curr=head;
ListNode *prev=NULL;

for(int i=0; i<left-1; i++) {
prev=curr;
curr=curr->next;
}

Before moving forward, we declare 2 pointers that will help connect the list together once the part of it is reversed.  First is a pointer that will point from the beginning part of the list to the start of the reversed part.

ListNode* list_to_reversal_start=prev;

We also declare a pointer that will connect the end of the reversed part of the list to the proper end of the list

ListNode* reversal_end_to_list=curr;

Now we iterate over the number of nodes we need to reverse.  First we declare a third pointer nextnode which points to the next node after current.  Then within the for loop, we set next node equal to current next, set current next to the previous node (reversing it), then set previous to current, and current to the next node.  We are using 3 pointers to move forward in the linked list while reversing it.

ListNode* nextnode=NULL;
for(int i=0; i<=right-left; i++) {
nextnode=curr->next;
curr->next=prev;
prev=curr;
curr=nextnode;
}

Once the part of the list is reversed, we connect it back with the original list.

If the start of the list pointer isn't null, we point the next node to the end of the reversed list.  If it is null, it means that the reversal asked to head node, so set head equal to the end of the reversed list. 

if(list_to_reversal_start!=NULL) {
list_to_reversal_start->next=prev;
} else {
head=prev;
}

Finally, set the end of the reversed list to the current node, the start of the unsorted list's end

reversal_end_to_list->next=curr;

Return head

return head;

In an interview it would be important to ask if swapping values is acceptable.  But prepare for the worst case.  This problem demonstrates how to use 3 pointers for moving forward while reversing the list.

Thanks to the Editorial for demonstrating 2 interesting solutions

Comments

Popular posts from this blog

Wednesday, September 20th

Monday, September 11th

Tuesday, September 19th