reverse a singly linked list in c

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

showing results for - "reverse a singly linked list in c"
Miguel
15 Jul 2018
1/**
2 * C program to reverse a Singly Linked List
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7
8
9/* Structure of a node */
10struct node {
11    int data; //Data part
12    struct node *next; //Address part
13}*head;
14
15
16/* Functions used in the program */
17void createList(int n);
18void reverseList();
19void displayList();
20
21
22int main()
23{
24    int n, choice;
25
26    /*
27     * Create a singly linked list of n nodes
28     */
29    printf("Enter the total number of nodes: ");
30    scanf("%d", &n);
31    createList(n);
32
33    printf("\nData in the list \n");
34    displayList();
35
36    /*
37     * Reverse the list
38     */
39    printf("\nPress 1 to reverse the order of singly linked list\n");
40    scanf("%d", &choice);
41    if(choice == 1)
42    {
43        reverseList();
44    }
45
46    printf("\nData in the list\n");
47    displayList();
48
49    return 0;
50}
51
52
53/*
54 * Create a list of n nodes
55 */
56void createList(int n)
57{
58    struct node *newNode, *temp;
59    int data, i;
60
61    if(n <= 0)
62    {
63        printf("List size must be greater than zero.\n");
64        return;
65    }
66
67    head = (struct node *)malloc(sizeof(struct node));
68
69    /*
70     * If unable to allocate memory for head node
71     */
72    if(head == NULL)
73    {
74        printf("Unable to allocate memory.");
75    }
76    else
77    {
78        /*
79         * Read data of node from the user
80         */
81        printf("Enter the data of node 1: ");
82        scanf("%d", &data);
83
84        head->data = data; // Link the data field with data
85        head->next = NULL; // Link the address field to NULL
86
87        temp = head;
88
89        /*
90         * Create n nodes and adds to linked list
91         */
92        for(i=2; i<=n; i++)
93        {
94            newNode = (struct node *)malloc(sizeof(struct node));
95
96            /* If memory is not allocated for newNode */
97            if(newNode == NULL)
98            {
99                printf("Unable to allocate memory.");
100                break;
101            }
102            else
103            {
104                printf("Enter the data of node %d: ", i);
105                scanf("%d", &data);
106
107                newNode->data = data; // Link the data field of newNode with data
108                newNode->next = NULL; // Link the address field of newNode with NULL
109
110                temp->next = newNode; // Link previous node i.e. temp to the newNode
111                temp = temp->next;
112            }
113        }
114
115        printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
116    }
117}
118
119
120/*
121 * Reverse the order of nodes of a singly linked list
122 */
123void reverseList()
124{
125    struct node *prevNode, *curNode;
126
127    if(head != NULL)
128    {
129        prevNode = head;
130        curNode = head->next;
131        head = head->next;
132
133        prevNode->next = NULL; // Make first node as last node
134
135        while(head != NULL)
136        {
137            head = head->next;
138            curNode->next = prevNode;
139
140            prevNode = curNode;
141            curNode = head;
142        }
143
144        head = prevNode; // Make last node as head
145
146        printf("SUCCESSFULLY REVERSED LIST\n");
147    }
148}
149
150
151/*
152 * Display entire list
153 */
154void displayList()
155{
156    struct node *temp;
157
158    /*
159     * If the list is empty i.e. head = NULL
160     */
161    if(head == NULL)
162    {
163        printf("List is empty.");
164    }
165    else
166    {
167        temp = head;
168        while(temp != NULL)
169        {
170            printf("Data = %d\n", temp->data); // Print the data of current node
171            temp = temp->next;                 // Move to next node
172        }
173    }
174}
Niko
09 Nov 2016
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
how to reverse linked list in pythonreverse linked list in cc how to reverse a linked listreverse linkedlistreverse the linked listlinked list reverse in creverse a linked list in c programreverse likned list determine the reversal of singly linked listhow we can display the elements of singly linked list in reverse order 28last element is display first 29 3fwrite a c program to reverse elements of a single linked lista function to return the reverse of a singly linked listhow to display a singly linked list in reverse orderreverse a singly linked list c 2b 2bwrite a c program to reverse a linked listreverse and return a singly linked listhow to reverse a linked list in c 2b 2breverse printing of linked list in creverse linked list chow to reverse a linked lisrtreverse linked list in c user defined inputreverse of linked listreverse singly linked listreverselist chow ot reverse a linked list in c 2b 2breverse a list in cwrite a program to reverse a string in stack using linked list in creverse in nodes in csingly linked list reverse c 2b 2bsingly linked list java reversec linked list reversereverse of a linked list in creverse a linked listreverse a linked list in creverse a linkedlist 28easy 29we 27ll cover the followingreverse linked list recursive pythonreversing a singly linked list in creversed linked listwrite a c program to implement a single linked list and display it in reverse orderreverse the given linked list in c 23reverse a linked list algorithmdisplay reverse in doubly linked list in c programreverse node list creverse of a linked listreverse linked list in c algorithmsprinting a linked list in reverse creverse of singly linked list in c 2b 2blinked list reversal problemc program to reverse of a linked listwrite a function to reverse a singly linked listrecursively reverse linked list pythonwrite a program to reverse a linked list print elements in singly linked list in reverse in creverse of a singly linked listhow to reverse a linke dlist in cc program to reverse linked listprogram to reverse a linked listhow to reverse singly linked list in c 2b 2btime complexity of reverse linked listto reverse a singly linked listreverse linked list iireverse a linked list using stack in cinvert a linked listrevert linked list in cfind the missing code to reverse a single linked list void reverse 28 29 7b node 2a current 3d head 3b node 2aprev 3d null 2c 2anext 3d null 3b while 28current 21 3d null 29 7b next 3d current 3enext 3b 2f missing code 2f current 3d next 3b 7d head 3d prev 3b 7dhow to reverse a linkedlistc 2b 2b display linked list using recursion in reversereverse the given linked list return a pointer to the head of the new reversed list reverse linked list without recursionc 2b 2bhow to reverser alinked listreverse ni 3dodescode that reverses a linked list in c 27write a program to print elements of a linked list in reverse order by using same single linked list 3fhow to iteratively reverse a linked listcan we reverse a linked list in chow to reverse singly linked listprint linked list backwards creverse a linked list swift first solutionreverse linked list c 2b 2b optimal solutionreverse a likedlistwrite a program to create a single linked list and display the node elements in reverse orderrecurisve way to reverse linked list c programreverse singly linked list functional how to reverse a linked list in creverse a doubly linked list in cin which linked list 2c the time complexity of the reversal operation will be constantreverse a link list in cdisplay reverse and indert in doubly linked list in c programwrite a function to reverse a single linked list in creverse order of linked listreverse a singly linked list in creverse singly linked list in creversing of linked listhow to reverse a linked list in c recursivly how to reverse a link list1 write a function to reverse a single linked list how to reverse linked list in chow to print a linked list in reverse order in creversing linked list in creversing a doubly linked list in chow to reverse a linked list chow to reverse a linked list in c using recursionconcept to reverse linked listc reverse linked listho to reverse a linked listwrite a program to reverse a linked list 3fdisplay doubly linked list in reverse order in cinverse a list in creverse link 5chow reverse linked list csingly linked list reversal crecursive linked list reverse in c92 reverse linked list iireverse a singly linked listwrite a program to reverse a linked list in c 2b 2breverse linked list recursive in cbest running time reseversing a singly linked listreverse linked listtraverse linked list in reverse orderreversing elements in a singly linked list in creverse linked list in place cwrite a c program to implement a singly linked list and display it in reverse orderhow do you reverse a linked list in place 3fdisplay reverse in singly linkedlist in creverse areverse lsingle inked listhow to reverse a singly linked list 3freverse linked list in c using recursionreversing a linked list in ca function that returns the reverse a given linked list in pythonreverselist link listreversing nodes in linked list in creverse sll in creverse the linked list in cc reverse listhow to reverse linked listc 2b 2b linked list reversereverse link list in creversing a linked listfunction to reverse a linked list in cimplement the reverse 28 29 method in singlylinkedlist 2c which reverses the linked list using only a constant amount of additional spaceprogam to reverse a linked list in c 2b 2bhow to reverse a list in creverse a single linked listflip linked listreverse singly linked list program in ccpdesignal reverse linked list pythonhow can we reverse a singly linked listcan a linked list reversecode to reverse a singly linked listtwo way linked list display in reverse order in cwrite a function to reverse a linked list c program make a new reversed linked listreversing a linked list iterativedescribe in detail an algorithm for reversing a singly linked list l using only a constant amount of additional space and not using any recursion linked list revesrse c 2b 2breverse a single linked list linkedlist reversewrite a c 2b 2b program to reverse a singly linked listwrite a c code to create and reverse a doubly linked list 2 2810 pts 29 write a method to reverse a linear linked list code for reversing a string using linked listhow to reverse the dirn of singly linked list creverse order of items in a linked list cbest way to reverse a linked listreversing a linked lsitreverse link list clinked lists method for reverse orderreverse linked list in c simplehow to reverse a linked listreverse of singly linked listsingly linked list reverse algorithmreverse linked list using recursion with classeshow to reverse a linker list in creversed link listgiven a singly linked list reverse and return itreverse a linked list using cgiven 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 reverse a linked list using single pointerhow to reverse a singly linked list chow to reverse linked list creverse a linked list creverse the linkedlistreverse linked list c 2b 2breverse link list using chow to reverse a given linked list c 2b 2bhow to reverse a singly linked listreverse linked list program in cfunction to reverselinked listwrite a function to reverse a singly linked list in creverse linked lisreverse ll algoprint reverse of linked list chow to reverse a singkly linked list 3fsingly linked list reversal in place csingly linked list reversehow to reverse a linked listc 2b 2bfunction tp reverse linked list in pythonwrite a program for reversing an the element in linked listgiven a singly linked list 2c reverse and return it reversing nodes in linked listlinked list reverse c 2b 2bwrite a program to create a single linked list and display the node elements in reverse order reverse a singly lined listwrite a c 2b 2b program to reverse a singly 2fdoubly link list also 2c write a function to print link list inthe same program how to reverse linked lists in creverse linked list algorithmreversing a linked list pythonreversing a linked list on creverse in place sllc program to reverse a linked listreverse the elements of a linked listprogram to reverse a linked list in cprint a linked list in reverse cnote 3a your solution should have o 28l length 29 time complexity and o 281 29 space complexity 2c since this is what you will be asked to accomplish in an interview given a singly linked list 2c reverse and return it reverse a linked list recursively pythonreversing chain linked listreverse a singly linked list pythonreverse a llinked list algorithmreverave a linked listsingly linked list program in creverse linked list inplaceiterative reverse linked listc program to reverse the link listinvert elements of a linked list in creverse nodesdisplay reverse linkedlist in cwrite a c program to reverse a linked list c 23invert listreverse a linked list c programlinked list in python 3 easy to understand nad code to rever a linked lit linked list reverse program in creverse a linked list in cgiven 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 complexityreverse of linked list in cfunction in c to reverse doubly linked listhow to reverse a doubly linked list in cprint linked list in reverse creverse of singly linked list in creversing the linked listsreverse the doubly linked list in creversing a linked list in python structuregiven a singly linked list pointed to by first 2c write a function that will invert the ordering of the nodes in the list reversing a singly linked list in data structurereverse singly linked list functionreverse linkgiven a singly linked list pointed to by first 2c write a function that will reverse the ordering of the nodes in the list revserse linkedlist in creverse a singly linked list in c