reverse a linked list

Solutions on MaxInterview for reverse a linked list by the best coders in the world

showing results for - "reverse a linked list"
Fanny
05 Apr 2019
1/*
2public class ListNode {
3    public int val;
4    public ListNode next;
5    public ListNode(int x) { val = x; next = null; }
6}
7*/
8
9public static ListNode[] reverse_linked_list(ListNode head) {
10
11        ListNode prev = null;
12        ListNode current = head;
13        ListNode next;
14
15        ListNode tail = head;
16
17        while (current != null) {
18
19            next = current.next;
20            current.next = prev;
21            prev = current;
22            current = next;
23        }
24
25        head = prev;
26
27        ListNode[] result = {head, tail};
28
29        return result;
30}
Clea
07 Jul 2019
1//Iterative program in C++ to reverse a linked list in groups of k
2
3//	(just before end of 2nd iteration of outer loop) [ 1->2->3->4->5->NULL ]
4// 1 <- 2    3 <- 4  5 ->NULL   (k=2)
5   ^		 ^		 ^
6   |		 |		 |
7prev_tail temp_head walker
8
9void reverse_by_k(Node **head,int k){
10    Node* temp_head = *head,*walker = *head,*prev_tail = NULL;
11    while(walker){
12        int i=0;
13        Node *temp = NULL,*prev = NULL;
14        //initialize temporary head to set previous tail later
15        temp_head = walker;
16        
17        //reverse group of k nodes
18        while(i<k && walker){
19            temp = walker->next;
20            walker->next = prev;
21            prev = walker;
22            walker = temp;
23            i++;
24        }
25        
26        if(prev_tail){
27            //previous tail has to point to temporary head of current group
28            prev_tail->next = prev;
29            prev_tail = temp_head;
30        } else{
31            prev_tail = *head;
32            *head = prev;
33        }
34    }
35}
Fabio
15 Oct 2020
1class recursion { 
2	static Node head; // head of list 
3	static class Node { 
4		int data; 
5		Node next; 
6		Node(int d) 
7		{   data = d; 
8			next = null; 	} } 
9	static Node reverse(Node head) 
10	{ 
11		if (head == null || head.next == null) 
12			return head; 
13		/* reverse the rest list and put the first element 
14        at the end */
15		Node rest = reverse(head.next); 
16		head.next.next = head; 
17		/* tricky step -- see the diagram */
18    	head.next = null; 
19		/* fix the head pointer */
20		return rest; 
21	}  /* Function to print linked list */
22	static void print() 
23	{ 
24		Node temp = head; 
25		while (temp != null) { 
26			System.out.print(temp.data + " "); 
27			temp = temp.next; 
28		} 
29		System.out.println(); 
30	} 
31	static void push(int data) 
32	{ 
33		Node temp = new Node(data); 
34		temp.next = head; 
35		head = temp; 
36	} /* Driver program to test above function*/
37public static void main(String args[]) 
38{ 
39	/* Start with the empty list */
40	push(20); 
41	push(4); 
42	push(15); 
43	push(85); 
44	System.out.println("Given linked list"); 
45	print(); 
46	head = reverse(head); 
47	System.out.println("Reversed Linked list"); 
48	print(); 
49} } // This code is contributed by Prakhar Agarwal 
Elena
26 Apr 2019
1	    /* Before changing next pointer of current node,
2        store the next node */
3        next = curr -> next
4        /*  Change next pointer of current node */
5        /* Actual reversing */
6        curr -> next = prev
7        /*  Move prev and curr one step ahead */
8        prev = curr
9        curr = next
10
Lilwenn
12 Nov 2018
1#include<bits/stdc++.h>
2 
3using namespace std;
4 
5struct node {
6    int data;
7    struct node *next;
8};
9 
10// To create a demo we have to construct a linked list and this 
11// function is to push the elements to the list. 
12void push(struct node **head_ref, int data) {
13    struct node *node;
14    node = (struct node*)malloc(sizeof(struct node));
15    node->data = data;
16    node->next = (*head_ref);
17    (*head_ref) = node;
18}
19 
20// Function to reverse the list
21void reverse(struct node **head_ref) {
22    struct node *temp = NULL;
23    struct node *prev = NULL;
24    struct node *current = (*head_ref);
25    while(current != NULL) {
26        temp = current->next;
27        current->next = prev;
28        prev = current;
29        current = temp;
30    }
31    (*head_ref) = prev;
32}
33 
34// To check our program 
35void printnodes(struct node *head) {
36    while(head != NULL) {
37        cout<<head->data<<" ";
38        head = head->next;
39    }
40}
41 
42// Driver function
43int main() {
44    struct node *head = NULL;
45    push(&head, 0);
46    push(&head, 1);
47    push(&head, 8);
48    push(&head, 0);
49    push(&head, 4);
50    push(&head, 10);
51    cout << "Linked List Before Reversing" << endl;
52    printnodes(head);
53    reverse(&head);
54    cout << endl;
55    cout << "Linked List After Reversing"<<endl;
56    printnodes(head);
57    return 0;
58}
59
Greta
14 Jun 2018
1#include <stdio.h>
2struct Node {
3   int data;
4   struct Node* next;
5   Node(int data){
6      this->data = data;
7      next = NULL;
8   }
9};
10struct LinkedList {
11   Node* head;
12   LinkedList(){
13      head = NULL;
14   }
15   void interReverseLL(){
16      Node* current = head;
17      Node *prev = NULL, *after = NULL;
18      while (current != NULL) {
19         after = current->next;
20         current->next = prev;
21         prev = current;
22         current = after;
23      }
24      head = prev;
25   }
26   void print() {
27      struct Node* temp = head;
28      while (temp != NULL) {
29         printf("%d ", temp-> data);
30         temp = temp->next;
31      }
32      printf("\n");
33   }
34   void push(int data){
35      Node* temp = new Node(data);
36      temp->next = head;
37      head = temp;
38   }
39};
40int main() {
41   LinkedList linkedlist;
42   linkedlist.push(85);
43   linkedlist.push(10);
44   linkedlist.push(65);
45   linkedlist.push(32);
46   linkedlist.push(9);
47   printf("Linked List : \t");
48   linkedlist.print();
49   linkedlist.interReverseLL();
50   printf("Reverse Linked List : \t");
51   linkedlist.print();
52   return 0;
53}
54
55
56Output
57Linked List : 9 32 65 10 85
58Reverse Linked List : 85 10 65 32 9
queries leading to this page
reverse a linked list rereverse a linked lsitjava node reverse orderreverse link 5citerative program to reverse linked listsinge linkedlist reverseprogramm to reverse a linked listreverse a linkedlidtreversing a link list cppprint reverse order singly linked listreversing singly linked listhow to reverse a singly linked list 3freverse linked list in c using recursionreturn a linked list javacan we reverse linked list locallyhow to print linked list in reverse orderreverse linked list in java functionreverse a linked list c 2b 2b recursiverevserse linkedlist in cconcept to reverse linked listlinked list reversingreverse linked list c 2b 3dreverse linked list codelinkedlist reverse c 2b 2bcreate a linked list and reverse itreverse linked linked listreverse linked list recursion c 2b 2bhow to revers a linked listcorrect reverse in linked list c 2b 2breverse a linkedlist in creturn reverse linked list javahow ot reverse linked listgeeks for geeks reverse linked list recursiveprint the linked list in reverse orderhow to reverse a linked list in creverse the linked list in pythonreverse of a linked list in javaeasy way to reverse a linked listreverse of linked listwrite a function that reverses a listint t linked list how to reverse a linked list in c 2b 2bcode to reverse a linked list method order java program to printalgorithem to reverse a linked listto reverse a linear linked listc 2b 2b code to reverse a linked listlinked list c 2b 2b reversereverse a linked list in jabfunction that will reverse the ordering of the nodes in the list reverse linked list examplereversse linked list javareversing the link listreverse in linked listreverse a linked list in javafunction to reverse a linked list in pythonreverse java linked listreverse link list creverse order linked listreverse a linked list in o 28n 29reverse linked list gfgreverse linked list in java to get both head and tailhow to reverse the single linked list 3freverse a linked list cppreverse and merge a linked list in c using ppointersprint linked list in reverse orderprint the linkedlist in reverse orderreverse method in linked listreverse k linked listreverse linkedlist classnodereverse method for linked list in javarecursive function to reverse singly linked list in c 2b 2breverse a ll in cpp recuresicelycan you reverse a linked list pythonreversing a linkedlisthow to reverse a linked list2 write a method to reverse a linear linked listreverse a linked list questionreverse listnode javawrite an algorithm to reverse a linked list reverse single linked listreverse linked list c 2b 2b recursionreverse a linked list leetcodereverse doubly linked list c 2b 2bfunction to reverse a doubly linked list c 2b 2bpush linkedlsit oppositereverse a linked lithow to invert a linked list in c 2b 2breverse a linked list efficientfunction to reverse a linked listreverse linked list revering linked listlinked list reverse pythonfunction that reverses a simple chained list in a single pass c 2b 2breverse an inner linked listreverse a single linked listreverse linked list in c 2b 2breverse linked loisthow do you reverse a linked list 3fhow to reverse singly linked listreverse linked list excersizereverese linked listjava reverse linked listlinked list reverse program in javareverse a linked list from given pointprinting reverse of linked listpublic node reverse 28node one 29 7b if 28one 3d 3d null 29 return null 3b if 28one next 3d 3d null 29 return one 3b node two 3d one next 3b node rest 3d reverse 28two 29 3b two next 3d one 3b one next 3d null 3b return rest next 3b 7dreverse in place sllreverse a singly linked list in creversing a doubly linked list in ck reverse a linked listreversing a singly linked listhow to reverse the linked listhow to reverse the dirn of singly linked list cprogram to implement the reverse of the linked list how to reverse a singly linked listhow to print a reversed linked list c 2b 2bprint in reverse linked listsreversing a linked list c 2b 2breverse linked list java methodhow to create a reversed linked list numswhen is reversing a linked list applicabledisplay reverse linkedlist in crevese the linked list javahow to return reversed linked list c 2b 2bhow to reverse a linked list creverse a linked list using stackreversed function in linked listprint reverse order singly linked list in c 2b 2breverse a linked list python1 reverse a linkedlistreverse and return linked listc 2b 2b reverse linked listreverse a linked list c 2b 2bhow to reverse a linked list in stllinked list in reverse orderreverse ll algowrite an algorithm to reverse the singly link list using recursionreverse a singly lined listreverse linked list explanationprogram to reverse a linked listwrite an algorithm to reverse a singly linked listreverse link list orderreverse linked lsitreverse linked list leetcodedoubly linked list reverserevese linked listlinkedlist reversereverse link list using creverse alinklist in c 2b 2bhow to flip a linked listhow to reverse a linkedlist c 2b 2breverse linked list c 2b 2b optimal solutionlinkedlist reverse javareverse a linked list in c 2b 2b practicehow to reverse linked listc 2b 2b code to reverse linked listreverse linkedlist class listnodeprint linked list in reverse creverse a linked list when we get a given valuehow to reversee a linked listreverse a linked list codereverse function for linked listsingly linked list java reversereverse of a linked listreverse a linked list leetcodegiven a singly linked list 2c reverse this linked list from position m to n do it in one pass you should try to do it in place the program should run in o 28nodes 29 time complexityfunction to reverse a doubly linked listsingly linked list 2c reverse a list 2c add a node 26 delete a nodereverse linked list js psuedo codewrite a program to reverse a linked list 3freverse linked list in kdescribe in detail an algorithm for reversing a singly linked list l using only a constant amount of additional space and not using any recursion c program to reverse the link listhow to print a singly linked list in a reverse orderhow to reverse a singly linked list using a void methodreversing a linked listinverse linked listreverse singly linked list in creverse a linked list javajava reverce a linked listcan a linked list reversereverse linkedin listlinkedlist element reversereverse linked list method javareverse linked list by kreverse a linkedlist gfgreverse a linked list using stack in c 2b 2breverse a linked listjava reverse link listvalues reverse in linked listrevesing a linked listreversed method in linked listreverave a linked listrevert linked list in creverse linked lsingly linked list reversehow to reverse the doubly linked list in c 2b 2breverse output of linked list in c 2b 2blinked list reverse algorithmreverse linked list in data structurereverse linked list whyorder of reversinng a linked listreverse a linked list hackerrank solution c 2b 2breverse the doubly linked list in creverse of linked list in cjava reverse a linked listcode to reverse to linked list method order java program to printhow to reverse a linke dlist in clinked list invertreverse a liked listreverse a linked list pseudocodehow to produce linked list in reverse orderreverse linked list in place javac 2b 2b reverse a linked listarray reverse example in linked listjava linked list reversewhat is reversing a linked listrecurisve way to reverse linked list c programjava linked list revese using nodesreverse each linked listreverse a linked list gfg solvedreverse a linked list algorithmreturn a pointer to the head of the new reversed list reverse linked list c 2b 2breverse a singly linked listfunction to reverse a linked list in javareverse a doubly linked list mmissing the end of the linked listreverse a doubly linked listin c 2b 2bwhat is the best way to reverse the linked listreverse linked list pythonwhat 27s the use of reverse linked listreverse linked list function c 2b 2bprogram to print linked list elementsbin reverse mannerreverse operation linked list explainedcan we reverse a linked list in o 28n 29java program to reverse a linked list with listiteratorreverse linked lishow to reverse singly linked list in c 2b 2bdisplay reverse in doubly linked list in c programflip list chow to reverse a linkedlistwhat is linked list and how to reverse itreverse of a linked lisreverse the given linked list write down a function to reverse a linked list destructively if the student id is even reverse the list using iteration cheggbest running time reseversing a singly linked listcorrect reverse in linked listreverses a simple 2c chained list in a single passreverse a linked list using single pointerreverse traverse linked listlinked list in java reversereverse linked list orderreverse node listjava reversing a linked listprint linked list reversereverse a linked list in placereverse linked list in javak reverse linked listrevere linkd list javareverse a linked listsjava linked list reverse libraryreverse print linked listdef reverselinkedlist 28l 29 3areverse order of linked list92 reverse linked list iito reverse a linked listreverse a linked list in c 2b 2brevesre a linked listreverse of a inked listprint given linked list in reversereversing linked listreersing a linked listreversing a doubly linked list in c 2b 2bcode to reverse lined listcan you reverse a linked list in o 28n 29specisal cases for reverseorder methjod in a linked list method in javagiven a singly linked list 2c reverse and return itgiven two linked lists show how to find that whether the data in one is reverse that of data in another no extra space should be used and traverse the linked lists only once linked list reverse functionsingly linked list reverse c 2b 2blinked list reverse c 2b 2bhow can we reverse a singly linked listreverse a linked liostreverse linked list in pythonreverse linked listreversed linked listhow to reverse a linked list c 2b 2bi just reversed a linked listjava reverse linkedlistto reverse a single linked listalgorithm reverse linked listreverse a singly linked list with a loopreverse a linked list coverreverse linked list in knidesinvert the order of a linked listreverse singly linked list c 2b 2blinked list in java reverse methoedreverse k nodes in linked listreverse link listdelete and reverse nodeshow to reverse a linked list in c recursivly how to reverse a linked list in c using recursionho to reverse a linked listreverse linked list lsingly linked list reversal creversing a linked list in c 2b 2breverse linked list in chow to reverese a linked listreverse a linked list reversevlyhow to invert a linked listreverse single linked list c 2b 2breverse linked list stlreverse a linked list in java using classesjava linked list reverseorderdisplay reverse linked listreverse the singly linked listreverse mid of linked listhow to reverse a single linked listreverse linked list iigiven a singly linked list pointed to by first 2c write a function that will reverse the ordering of the nodes in the list what is reverse linked listreverse a linked list explanationhow to reverse the linked list 3frevres linked listreverse linked list without recursionteverse linked listreverse linked list in o 28n 29reverse singly linked listfunction to reverse single linked listrevers linked listreverse a linked list 5ceverse a linked list vs reverse an arrayrecerse a linked listreverse linked list recursive in creveerse linked list pythondeallocating a linked list after reversing it c 2b 2bwrite a c program to implement a singly linked list and display it in reverse ordergiven a singly linked list 2c return another linked list that is the reverse of the first reverse a given linked listreverse linkedlistwrite a program to reverse a linked list in java javascripthow to reverse a singly linked list c 2b 2bgiven a singly linked list 2c reverse and return it pythonnode reverse in javareverse linked list in sets of kreverse linked list in cpp using stackto reverse a singly linked listreverse a linked list in place recursively and return the new head pointerreverse a linked list in c 2b 2b stlreverse k blocks in linked listwrite a program to create a single linked list and display the node elements in reverse orderlinked list reverse in cgiven a singly linked list reverse and return itreverse a linklistreverse linkedlist in javahow to reverse a lnked list in javareverse a doubly linked list in c 2b 2breverse a linked list stlbest algorithm to reverse a linked listc 2b 2b reverse linked list iterreverse stl c 2b 2b for linked listwrite a non recursive routine to reverse a singly linked list in o 28n 29 timeheaded elements are reversed how to fix italgorithm for reverse a linked listhow to reverse a singly linked list using recursion pythonc 2b 2b linked list reversereverse linked list cppreverse linked list program with returning valueshow to reverse a doubly linked list in c 2b 2bhow to reverse values in linked list c 2b 2bprogram to print linked list in reverse orderreversing linked list in creverse a linkedlistprint linked list in reversereverse node codereversing nodes in linked list in creversing the linked listlinked lsit reverse functionreverse od a doublyh linkedlist c 2b 2breverse linked list iterativereverse a linked list in creversek nodes in linked listcreate a reverse linked list problemreverse a doubly linked listreverse linked list ii solutionreverse a linked list in pythonbest way to reverse a linked list in pythonhow to reverse linked list in javareversing a singly linked list in c 2b 2breverse ll iterative javajava program to reverse a linked listreversee a linked listreverse linked list javahow to create a linked list representing a long reversehow to reverse order in jave linked listreverse the linked list in chow do you reverse a linked list in placejava program to reverse a linked list listiteratorhow to reverse a liinked listhow to reverse a doubly linked listreverse of the linked listreverse node list cprint reverse linked listreverse linked list iterative in javareverse sublist of linked listreverse the linked list c 2b 2bc program to reverse linked listreversing the linked listsreversed linked list c 2b 2bfungsi reverse linked listreverse linked isthow do you reverse a linked list in place 3freverse k elements in linked listreverse a linked list in a efficient waywrite a program to create a single linked list and display the node elements in reverse order reverse print linked list c 2b 2bgiven a reference to the head node of a singly linked list 2c write a function that reverses the linked list in place the function should return the new head of the reversed list how to reverse a linked list in pythonc 2b 2b reverse linked list iterativelyreverse of linked list in pythonspecisal cases for reverseorder methjod in a linked list method javalinked list reverselinked list remove node by parameter traverse backwardsreverse linked list in placereversing a linked list javahow to reverse a linked list in javadisplay reverse and indert in doubly linked list in c programprint elements in singly linked list in reverse in chow to reverse a link listreverse linked list 3reverse linked list i leetcodeinvert linked listbest way to reverse linked listwrite a c program to implement a single linked list and display it in reverse orderprinting linked list in reverse order c 2b 2bsingly list in reverse pythonreverse a list in java using just the headreverse the linked listpython reverse function linked listhow to print reverse singly linked listreverse a linked list in hackerrankreverse linked list iterative c 2b 2breverse linkd list c 2b 2breverse list algorithmlinked list how to reversereverse method for linkedlist in javahow to reverse a lined listhow ot reverse a linked list in c 2b 2breversing linked list c 2b 2bgiven a singly linked list reverse and return it pythonreversea a linked listreverse linked list algorithmprinting the linked list in reversedisplay reverse in singly linkedlist in chow to reverse a given linked list c 2b 2bwrite a c program to reverse a linked list reverse linked list recursive c 2b 2breverrsing a linked listreverse a linked list in cppprogram to reverse linked listreverse and merge a linked list in c using pointerslinked list reversalconcept of how to reverse linked listreverse a linke dlist in javalinked list reverse javahow to display a reversed linked list in c 2b 2breverse recursive linked listreverse linked list geeksforgeeksprint a linked list in reversehow to reverse a linked list javareverse a linked list c programreverse linked list kreverse of a linked list in cgfg reverse the linked listreturn a reversed linked list gfghow o reverse a linkd listreverse a linked list in o 28n 29 time reverse a linked list gfgreverse a linkde listreverse a linked list in o 28n 29 timerevesrse linked listgiven a singly linked list 2c reverse and return it linked list k reverse c 2b 2breverse a linked list