Answers for "what is reverse linked list"

C++
2

Reverse a linked list c++

#include<bits/stdc++.h>
 
using namespace std;
 
struct node {
    int data;
    struct node *next;
};
 
// To create a demo we have to construct a linked list and this 
// function is to push the elements to the list. 
void push(struct node **head_ref, int data) {
    struct node *node;
    node = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->next = (*head_ref);
    (*head_ref) = node;
}
 
// Function to reverse the list
void reverse(struct node **head_ref) {
    struct node *temp = NULL;
    struct node *prev = NULL;
    struct node *current = (*head_ref);
    while(current != NULL) {
        temp = current->next;
        current->next = prev;
        prev = current;
        current = temp;
    }
    (*head_ref) = prev;
}
 
// To check our program 
void printnodes(struct node *head) {
    while(head != NULL) {
        cout<<head->data<<" ";
        head = head->next;
    }
}
 
// Driver function
int main() {
    struct node *head = NULL;
    push(&head, 0);
    push(&head, 1);
    push(&head, 8);
    push(&head, 0);
    push(&head, 4);
    push(&head, 10);
    cout << "Linked List Before Reversing" << endl;
    printnodes(head);
    reverse(&head);
    cout << endl;
    cout << "Linked List After Reversing"<<endl;
    printnodes(head);
    return 0;
}
Posted by: Guest on June-20-2021
2

Reverse a Linked List

 /* Before changing next pointer of current node,
        store the next node */
        next = curr -> next
        /*  Change next pointer of current node */
        /* Actual reversing */
        curr -> next = prev
        /*  Move prev and curr one step ahead */
        prev = curr
        curr = next
Posted by: Guest on June-30-2021
1

reverse linked list by k

//Iterative program in C++ to reverse a linked list in groups of k

//	(just before end of 2nd iteration of outer loop) [ 1->2->3->4->5->NULL ]
// 1 <- 2    3 <- 4  5 ->NULL   (k=2)
   ^		 ^		 ^
   |		 |		 |
prev_tail temp_head walker

void reverse_by_k(Node **head,int k){
    Node* temp_head = *head,*walker = *head,*prev_tail = NULL;
    while(walker){
        int i=0;
        Node *temp = NULL,*prev = NULL;
        //initialize temporary head to set previous tail later
        temp_head = walker;
        
        //reverse group of k nodes
        while(i<k && walker){
            temp = walker->next;
            walker->next = prev;
            prev = walker;
            walker = temp;
            i++;
        }
        
        if(prev_tail){
            //previous tail has to point to temporary head of current group
            prev_tail->next = prev;
            prev_tail = temp_head;
        } else{
            prev_tail = *head;
            *head = prev;
        }
    }
}
Posted by: Guest on September-20-2021
0

linked list reverse

// using iterative method to reverse linked list in JavaScript
// time complexity: O(n) & space complexity: O(1)
reverse() {
      if (!this.head.next) {
        return this.head;
      }
      
      let prevNode = null;
      let currNode = this.head;
      let nextNode = this.head;
      while(nextNode){
        nextNode = currNode.next;
        currNode.next = prevNode;
        prevNode = currNode;
        currNode = nextNode;
      }
      this.head = prevNode;
      return this.printList();
    }
Posted by: Guest on June-17-2021

Code answers related to "what is reverse linked list"

Browse Popular Code Answers by Language