Reverse a Linked List Hackerrank Solution in C++Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty.
Example
head references the list 1 -> 2 -> 3 -> NULL
Manipulate the next pointers of each node in place and return the head, now referencing the head of the list 3 -> 2 -> 1 -> NULL.
Function Description
Complete the reverse function in the editor below.
reverse has the following parameter:
SinglyLinkedListNode pointer head: a reference to the head of a list
Returns
SinglyLinkedListNode pointer: a reference to the head of the reversed list
Input Format
The first line contains an integer t, the number of test cases.
Each test case has the following format:
The first line contains an integer n, the number of elements in the linked list.
Each of the next n lines contains an integer, the data values of the elements in the linked list.
Constraints
1 <= t <= 10
1 <= n <= 1000
1 <= list[i] <= 10, where list[i] is the ith element in the list.
Sample Input
1
5
1
2
3
4
5
Sample Output
5 4 3 2 1
Explanation
The initial linked list is 1 -> 2 -> 3 -> 4 -> 5 -> NULL.
The reversed linked list is: 5 -> 4 -> 3 -> 2 -> 1 -> NULL.
Submit Your Solution Here: Click Here
Reverse a Linked List Hackerrank Solution in C++
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode
{
public:
int data;
SinglyLinkedListNode * next;
SinglyLinkedListNode(int node_data)
{
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList
{
public:
SinglyLinkedListNode * head;
SinglyLinkedListNode * tail;
SinglyLinkedList()
{
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(int node_data)
{
SinglyLinkedListNode *node = new SinglyLinkedListNode(node_data);
if (!this->head)
{
this->head = node;
}
else
{
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode *node, string sep, ofstream &fout)
{
while (node)
{
fout << node->data;
node = node->next;
if (node)
{
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode *node)
{
while (node)
{
SinglyLinkedListNode *temp = node;
node = node->next;
free(temp);
}
}
/*
*Complete the 'reverse' function below.
*
*The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
*The function accepts INTEGER_SINGLY_LINKED_LIST llist as parameter.
*/
/*
*For your reference:
*
*SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
*};
*
*/
SinglyLinkedListNode* reverse(SinglyLinkedListNode *llist)
{
SinglyLinkedListNode *present = llist;
SinglyLinkedListNode * future;
SinglyLinkedListNode *past = nullptr;
while (present != nullptr)
{
future = present->next;
present->next = past;
past = present;
present = future;
}
return past;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int tests_itr = 0; tests_itr < tests; tests_itr++)
{
SinglyLinkedList *llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++)
{
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
llist->insert_node(llist_item);
}
SinglyLinkedListNode *llist1 = reverse(llist->head);
print_singly_linked_list(llist1, " ", fout);
fout << "\n";
free_singly_linked_list(llist1);
}
fout.close();
return 0;
}
Reverse a Linked List Explanation
Before solving this problem (reverse a linked list) first, see how linked list work for better understand this problem I have created a Full Code of a Reverse Linked List so you can understand very well First check the full code and test. After that see the hacker rank solution for reverse a linked list function or segment, and paste that coloured code in the editor for a successfully compiled solution.
The Output of Reverse a Linked List Hackerrank Solution
Similar to Reverse a Linked List
- Input And Output Hackerrank Solution in C++
- Solve Me First Hackerrank Solution in C & C++
- A Very Big Sum Hackerrank Solution C++
- Deque STL Hackerrank Solution in C++
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
- For Loop Hackerrank Solution in C++
- Functions in C++ Hackerrank Solution
- Pointer Hackerrank Solution in C++
- Arrays Introduction Hackerrank Solution in C++
- Strings Hackerrank Solution in C++
- Plus Minus Hackerrank Solution C++
- Hello World in C Hackerrank Solution
- Divisible Sum Pairs Hackerrank Solution in C++
- Apple And Orange Hackerrank Solution
- Simple Array Sum Hackerrank Solution C++
- Compare The Triplets Hackerrank Solution C++
0 Comments: