Print linked list in reverse hackerrank
June 15, 2020 miraclemaker HackerRank 4 Show We have to print a Linked List in Reverse order. For example: 5 // number of elements of Linked List 1 2 3 4 5The linked List will be: 1->NULL 1->2->NULL 1->2->3->NULL 1->2->3->4->NULL 1->2->3->4->5->NULLWe have to Print Like Output 5 4 3 2 1Here, I have represented the logic of the Print in Reverse using Recursion. Please Dry and Run the code for the better Understanding. void reversePrint(SinglyLinkedListNode* head) {
if(head==NULL)
{return ;
}
else
{
reversePrint(head->next);
cout< Insert node at position : HackerRank Solution in C++ Insert Node at head : HackerRank Solution in C++ Insert node at tail : HackerRank Solution in C++ Cycle Detection: HackerRank Solution in C++ Hacker Rank Solutions: Find Merge Point of Two Lists Hacker Rank Solution: Print the Elements of a Linked List Hacker Rank Solution: Merge two sorted linked lists Sparse Arrays : HackerRank Solution in C++ Left Rotation : HackerRank Solution in C++ Related
X
This site uses cookies. By continuing, you agree to their use. Learn more, including how to control cookies. You are given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty – in that case, do not print anything! Input Format You have to complete the void reversePrint(SinglyLinkedListNode head) method which takes one argument – the head of the linked list. You should NOT read any input from stdin/console. The first line of input contains t, the number of test cases.
Constraints
Output Format Complete the reversePrint function in the editor below and print the elements of the linked list in the reverse order, each in a new line. Sample Input 3 5 16 12 4 2 5 3 7 3 9 5 5 1 18 3 13 Sample Output 5 2 4 12 16 9 3 7 13 3 18 1 5 Explanation There are three test cases. Iterative approach: Initialize three pointers previous as NULL, current as head and next as NULL. next = current->next // Now change next of current// This is where actual reversing happenscurrent->next = previous // Move prev and curr one step forwardprevious = current Code: static void reversePrint(SinglyLinkedListNode head) { SinglyLinkedListNode previous = null; SinglyLinkedListNode next = null; SinglyLinkedListNode current = head; while(current != null) { next = current.next; current.next = previous; previous = current; previous = next; } while(previous != null) { System.out.println(previous.data); previous = previous.next; } Hope you guys understand the simple solution of reverse a linked list in java. In next blog we will see other operations on linked list till then Bye Bye..! Thanks for reading…! Happy Coding..! Thank You…!
Print in Reverse
In C:
//the following fuction is all that is needed to complete the
//challenge in hackerrank platform.
void reversePrint(SinglyLinkedListNode* head) {
SinglyLinkedListNode* current=head,*next,*prev=NULL;
while(current!=NULL)
{ next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
SinglyLinkedListNode* temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
In C++:
//the following fuction is all that is needed to complete the
//challenge in hackerrank platform.
void ReversePrint(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
if(head!=NULL)
{
ReversePrint(head->next);
cout<
In this HackerRank Print in Reverse problem, we need to develop a program in which if we have given a pointer to the head of a singly linked list then we need to print each value of the linked list in the reversed order. and if the given list is empty, then we don't need to print anything. Problem solution in Python programming.#!/bin/python3 import math import os import random import re import sys class SinglyLinkedListNode: def __init__(self, node_data): self.data = node_data self.next = None class SinglyLinkedList: def __init__(self): self.head = None self.tail = None def insert_node(self, node_data): node = SinglyLinkedListNode(node_data) if not self.head: self.head = node else: self.tail.next = node self.tail = node def print_singly_linked_list(node, sep): while node: print(node.data, end='') node = node.next if node: print(sep, end='') def reversePrint(head): elements = [] while(head != None): elements.append(head.data) head=head.next elements = elements[::-1] for element in elements: print (element) if __name__ == '__main__': tests = int(input()) for tests_itr in range(tests): llist_count = int(input()) llist = SinglyLinkedList() for _ in range(llist_count): llist_item = int(input()) llist.insert_node(llist_item) reversePrint(llist.head)Problem solution in Java Programming.import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { static class SinglyLinkedListNode { public int data; public SinglyLinkedListNode next; public SinglyLinkedListNode(int nodeData) { this.data = nodeData; this.next = null; } } static class SinglyLinkedList { public SinglyLinkedListNode head; public SinglyLinkedListNode tail; public SinglyLinkedList() { this.head = null; this.tail = null; } public void insertNode(int nodeData) { SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData); if (this.head == null) { this.head = node; } else { this.tail.next = node; } this.tail = node; } } public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep) { while (node != null) { System.out.print(node.data); node = node.next; if (node != null) { System.out.print(sep); } } } static void reversePrint(SinglyLinkedListNode head) { if (head == null) return; // print list of head node reversePrint(head.next); // After everything else is printed System.out.println(head.data+" "); } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int tests = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int testsItr = 0; testsItr < tests; testsItr++) { SinglyLinkedList llist = new SinglyLinkedList(); int llistCount = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int i = 0; i < llistCount; i++) { int llistItem = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); llist.insertNode(llistItem); } reversePrint(llist.head); } scanner.close(); } } #includeProblem solution in JavaScript programming.'use strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { inputString = inputString.replace(/\s*$/, '') .split('\n') .map(str => str.replace(/\s*$/, '')); main(); }); function readLine() { return inputString[currentLine++]; } const SinglyLinkedListNode = class { constructor(nodeData) { this.data = nodeData; this.next = null; } }; const SinglyLinkedList = class { constructor() { this.head = null; this.tail = null; } insertNode(nodeData) { const node = new SinglyLinkedListNode(nodeData); if (this.head == null) { this.head = node; } else { this.tail.next = node; } this.tail = node; } }; function printSinglyLinkedList(node, sep) { while (node != null) { process.stdout.write(String(node.data)); node = node.next; if (node != null) { process.stdout.write(sep); } } } function reversePrint(head) { if (head !== null) { reversePrint(head.next); console.log(head.data) } } function main() { |