reverse a linked list using recursion
C++
// Recursive C++ program to reverse
// a linked list
#include <iostream>
using namespace std;
/* Link list node */
struct Node {
int data;
struct Node* next;
Node(int data)
{
this->data = data;
next = NULL;
}
};
struct LinkedList {
Node* head;
LinkedList()
{
head = NULL;
}
/* Function to reverse the linked list */
Node* reverse(Node* node)
{
if (node == NULL)
return NULL;
if (node->next == NULL) {
head = node;
return node;
}
Node* node1 = reverse(node->next);
node1->next = node;
node->next = NULL;
return node;
}
/* Function to print linked list */
void print()
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
void push(int data)
{
Node* temp = new Node(data);
temp->next = head;
head = temp;
}
};
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
LinkedList ll;
ll.push(20);
ll.push(4);
ll.push(15);
ll.push(85);
cout << "Given linked list\n";
ll.print();
ll.reverse(ll.head);
cout << "\nReversed Linked list \n";
ll.print();
return 0;
}
Also in C++:
- Title
- level order traversal
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- array declaration c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- length of string in c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- howt o initialize 3d vector in c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- sqrt in c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- stack c++
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- how to swap string characters in c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- Newton's sqrt in c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- How to check if a triangular cycle exists in a graph
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- qt graphics scene map cursor position
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- c++ round to int
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- c++ remove element from vector
- Category
- C++
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++