Swap reversing a linked list in c

reverse a linked list
swapping two nodes in a doubly linked list c
swap adjacent nodes in linked list
reverse a linked list in c#
swapping two nodes in a singly linked list c
swapping two nodes in a doubly linked list java
algorithm to swap two nodes in a singly linked list
reverse a doubly linked list

Given a list, I want to reverse the list by swapping the first with the last, second with the second last and so on.

I wrote this function to swap each pair where pos1 and pos2 are the two positions to be swapped.

maxPos is the largest of the two positions, node1 and node2 are the two nodes that were found after traversing the list.

int swap(struct node *list, int pos1, int pos2) {

    if (node1 != NULL && node2 != NULL) {
        if (prev1 != NULL)
            prev1->next = node2;
        if (prev2 != NULL)
            prev2->next = node1;
        temp        = node1->next;
        node1->next = node2->next;
        node2->next = temp;
        if (prev1 == NULL)
            head = node2;
        else if (prev2 == NULL)
            head = node1;
    }
    return 1;
}

Instead of calling this recursively for each pair, ie. (1,n-1), (2,n-2), (3,n-3) for which it has to traverse the list every time, I was wondering if there is a way for it to be solved iteratively.

You'll have to declare a new method for reversing the whole list that'll be called only once. Also you'd better use a doubly-linked list for this problem.

Node structure:

struct node {
    int data;
    struct node *next;
    struct node *prev;
};

Method:

void reverse() {

    struct node *temp = head; // assuming you have the head node as a global variable.
    struct node *lastPtr = head;

    for (int i = 0; i < len; i++) { // assuming you have length as a global variable.
        lastPtr = lastPtr->next;
    }

    for (int i = 0; i < len / 2; i++) {
        temp->data += lastPtr->data;
        lastPtr->data = temp->data - lastPtr->data;
        temp->data -= lastPtr->data;
        temp = temp->next;
        lastPtr = lastPtr->prev;
    }
}

Swap nodes in a linked list without swapping data, Following are C and Java implementations of this approach. C++; C; Java; Python; C#. C++.Duration: 3:17 Posted: Aug 13, 2017 Steps to reverse a Singly Linked List. Create two more pointers other than head namely prevNode and curNode that will hold the reference of previous node and current node respectively. Make sure that prevNode points to first node i.e. prevNode = head.

Do you really want to swap node contents?

You can reverse the list iteratively with a very simple function:

struct node {
    // whatever payload fields...
    struct node *next;
};

struct node *reverse_list(struct node *list) {
    struct node *last = NULL;
    while (list != NULL) {
        struct node *next = list->next;
        list->next = last;
        last = list;
        list = next;
    }
    return last;
}

Reverse a Doubly Linked List, In contrast, the pointer to the node will always be simpler data type and either of 4 or 8 bytes. Below is the implementation of the algorithm. C++; Java; Python3; C# � Recursive Method: 1) Divide the list in two parts - first node and rest of the linked list. 2) Call reverse for the rest of the linked list. 3) Link rest to first.

How To Reverse a Linked List (3 Different Ways), In this method, we will swap linked list node objects (references to the data). Swapping starts from the first node's object and the first node's object is swapped with the last node's object. Then, the second node's object is swapped with the one before the last node's object. Reversing a Linked List. Let us dive right into the discussion for the solution. We will discuss two methods: Iterative Solution (using 3 pointers) Recursive Solution (using pseudo-2 pointers) Note: I would suggest you to try to solve the problem, and then go to the solution. Reverse a Linked List using Iterative Solution

Reversing a Linked List: Easy as 1, 2, 3 | by Sergey Piterman, The first is to start from the head and the tail, and swap the elements in the list, as if you were operating on an array. This would be done with two� If a linked list is reversed by changing its link between nodes then its head node will become tail node and its tail node will become head node. As the linked list is reversed its previous tail node i.e. the last node will no more point to NULL. Now previous head node that is the first node will point to NULL.

C program to swap two nodes in a linked list, Recommended programs to practice. C program to swap two numbers. C program to search an element in linked list. C program to reverse a� So reversing a linked list, simply means reassigning all the next properties, on every node. Swap nodes in a linked list without swapping data - GeeksforGeeks.

Reverse Linked List II, Read LeetCode's official solution for Reverse Linked List II. We repeatedly swap elements pointed to by these two pointers and we move both we can't reach the node C . That's why we need a third pointer that will help us� Reversing of a linked list means that the head pointer will point to the terminal node of the linked list or the last node of the linked list and the existing head will be pointing towards NULL. This can be easily done with the help of three pointers where in with each traversal through the linked list they keep on reversing the pointer to the

Comments
  • Please make a minimal reproducible example of the code working recursively, to provide a foundation for asking about the alternative solution. As is, there is not enough information on several details in your program. E.g. typedefs.
  • @Yunnosch I've cleaned it to be readable as much as I can. Thank you.
  • @Fredrik, I'm not sure how I can use a for loop in this case. Like the first and last element?
  • calling this recursively for each pair, ie. (1,n-1), (2,n-2), (3,n-3) that is not usually called recursively. A recursion might remember the outer nodes, apply itself to the list without those, and prepend the last, append the first node for its result. BUT: Given there is a nice iterative procedure to reverse a singly linked list, why create an inferior recursive one?
  • @Fredrik say the head is written as void **head and the memory have been allocated how will the for loop look like?
  • @MuhammedGui temp->data += lastPtr->data; what does this do? is it adding the value in the temp and lastPtr?
  • @JDoe Yes it is. That line and the following 2 lines are for swapping the datas between the two nodes.
  • So how does the struct node look like in this case? struct node{ int data; struct node *next;} am I correct.
  • I assumed you are using a doubly-linked list since i used 'lastPtr->prev;'. So the struct node must be like: { int data; struct node *next; struct node *prev; } where 'prev' points to the previous node.
  • you do not answer to the question, you do not swap the cells but their values