21/03/2023

Reverse a Linked List Hackerrank Solution | Data Structures

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.

Reverse a Linked List Hackerrank Solution in C++

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


The Output of Reverse a Linked List Hackerrank Solution

Similar to Reverse a Linked List


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: