Swapping adjacent elements of linked list

前端 未结 4 599
执念已碎
执念已碎 2021-01-16 09:50

The below is my code to recursive swap the adjacent elements of a linked list. I am losing the pointer to every second element after the swap. The input is 1->2->3->4->5->6-

相关标签:
4条回答
  • 2021-01-16 10:25

    With no recursion:

    void swap(node **head)
    {
        while (*head && (*head)->next)
        {
            node* tmp = *head;
            *head = tmp->next;
            tmp->next = (*head)->next;
            (*head)->next = tmp;
            head = &tmp->next;
        }
    }
    

    Invoke swap( & list_head_ptr).


    Alternatively, you can pass the head pointer by reference-to-pointer and utilize a local pointer-to-pointer member:

    void swap(node*& head)
    {
        node **pp = &head;
        while (*pp && (*pp)->next)
        {
            node* tmp = *pp;
            *pp = tmp->next;
            tmp->next = (*pp)->next;
            (*pp)->next = tmp;
            pp = &tmp->next;
        }
    }
    

    and invoke as swap(list_head_ptr). Either method works.

    0 讨论(0)
  • 2021-01-16 10:27

    Using recursion:

    void nodelist::swap(node** head) {
      if (!*head || !(*head)->next) return;
      node* const sw = (*head)->next;
      (*head)->next = sw->next;
      sw->next = *head;
      *head = sw;
      swap(&(sw->next->next));
    }
    
    0 讨论(0)
  • 2021-01-16 10:40

    In fact it is enough to swap only the data members of nodes. There is no need to swap the pointers themselves.

    Nevertheless if to use your approach then the function can look like

    void SwapList( node *head )
    {
        if ( head != nullptr && head->next != nullptr )
        {
            node *next = head->next;
            std::swap( *head, *next );
            std::swap( head->next, next->next );
    
            SwapList( head->next->next );
        }
    }
    

    Here is a demonstrative program

    #include <iostream>
    #include <utility>
    
    struct node
    {
        int value;
        node *next;
    };
    
    node * AddNode( node *head, int value )
    {
        head = new node { value, head };
    
        return head;
    }
    
    void PrintList( node *head )
    {
        for ( ; head != nullptr; head = head->next )
        {
            std::cout << head->value << ' ';
        }
    }
    
    void SwapList( node *head )
    {
        if ( head != nullptr && head->next != nullptr )
        {
            node *next = head->next;
            std::swap( *head, *next );
            std::swap( head->next, next->next );
    
            SwapList( head->next->next );
        }
    }
    
    int main() 
    {
        node *head = nullptr;
    
        for ( int i = 10; i != 0; )
        {
            head = AddNode( head, --i );
        }
    
        PrintList( head );
        std::cout << std::endl;
    
        SwapList( head );
    
        PrintList( head );
        std::cout << std::endl;
    
        return 0;
    }
    

    The output is

    0 1 2 3 4 5 6 7 8 9 
    1 0 3 2 5 4 7 6 9 8 
    

    You can use the shown function as a template (or base) for your function.

    0 讨论(0)
  • 2021-01-16 10:41

    If head is the pointer which stores the address of firstNode (value=1), then try following function:

    void nodelist::swap(node* head){
        node* temp = head->next; //head->next is first-node which needs to switch with it's next node
        if (temp!= nullptr && temp->next!=nullptr){
            head->next=temp->next;  //move second node to first
            temp->next = head->next->next; //put second's next in first's
            head->next->next = temp; //and first will be second's next
            temp = nullptr; // swaping done
            swap(head->next->next); //do it for next couple
        }
    }
    

    http://coliru.stacked-crooked.com/a/e1cc0d02b5599da4

    OR

    http://coliru.stacked-crooked.com/a/a1e200b687825d80

    If head itself is the firstNode (value=1), then passing head by value will not work, either you need to pass it by address/reference OR do it like in following link:

    http://coliru.stacked-crooked.com/a/a1e200b687825d80

    0 讨论(0)
提交回复
热议问题