# Print reverse of a linked list without actually reversing

That is a linked list traversal problem, Displaying the linked list node in reverse order without effect (modified) in original linked list. Lets see few example.

``````Example A
Input List : 1 → 2 → 8 → 4 → 9 → 6 → NULL
Output     : 6   9   4   8   2   1
Example B
Input List : 1 → 2 → 3 → NULL
Output     : 3   2   1``````

This problem are targeted to understand iterative and recursive mechanism in programming language. Because recursion is simplest solution of this problem. And iterative is based on stack data structure. This post are based on recursive solution.

``````/*
C program for
Print reverse of a linked list without actually reversing
*/
#include <stdio.h>
#include <stdlib.h>

{
int data;
};
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll =
(struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->tail = NULL;
}
return sll;
}
void appendNode(struct SingleLL *sll, struct LinkNode *node)
{
{
}
else
{
// Append the node at last position
sll->tail->next = node;
}
sll->tail = node;
}
void addNode(struct SingleLL *sll, int data)
{
// Create dynamic node
if (node == NULL)
{
return;
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
appendNode(sll, node);
}
// Display Reversal view of linked list using recursion
{
if (node == NULL)
{
return;
}
// Visit to next node
printReverse(node->next);
// Display node value
printf("  %d", node->data);
}
int main()
{
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
return 0;
}``````

#### Output

``  6  9  4  8  2  1``
``````// Java Program
// Print reverse of a linked list without actually reversing

{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display Reversal view of linked list using recursion
{
if (node == null)
{
return;
}
// Visit to next node
this.printReverse(node.next);
// Display node value
System.out.print(" " + node.data);
}
public static void main(String[] args)
{
SingleLL sll = new SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}
}``````

#### Output

`` 6 9 4 8 2 1``
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Print reverse of a linked list without actually reversing

{
public:
int data;
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
public:
SingleLL()
{
this->tail = NULL;
}
{
{
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
// Display Reversal view of linked list using recursion
{
if (node == NULL)
{
return;
}
// Visit to next node
this->printReverse(node->next);
// Display node value
cout << " " << node->data;
}
};
int main()
{
SingleLL *sll = new SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
return 0;
}``````

#### Output

`` 6 9 4 8 2 1``
``````// Include namespace system
using System;
// Csharp Program
// Print reverse of a linked list without actually reversing

{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display Reversal view of linked list using recursion
{
if (node == null)
{
return;
}
// Visit to next node
this.printReverse(node.next);
// Display node value
Console.Write(" " + node.data);
}
public static void Main(String[] args)
{
SingleLL sll = new SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}
}``````

#### Output

`` 6 9 4 8 2 1``
``````package main
import "fmt"
// Go Program
// Print reverse of a linked list without actually reversing

data int
}
me.data = data
me.next = nil
return me
}
type SingleLL struct {
}
func getSingleLL() * SingleLL {
var me *SingleLL = &SingleLL {}
me.tail = nil
return me
}
} else {
// Append the node at last position
this.tail.next = node
}
this.tail = node
}
// Display Reversal view of linked list using recursion
func(this SingleLL) printReverse(node * LinkNode) {
if node == nil {
return
}
// Visit to next node
this.printReverse(node.next)
// Display node value
fmt.Print(" ", node.data)
}
func main() {
var sll * SingleLL = getSingleLL()
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}``````

#### Output

`` 6 9 4 8 2 1``
``````<?php
// Php Program
// Print reverse of a linked list without actually reversing

{
public \$data;
public \$next;
public	function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
class SingleLL
{
public \$tail;
public	function __construct()
{
\$this->tail = NULL;
}
{
{
}
else
{
// Append the node at last position
\$this->tail->next = \$node;
}
\$this->tail = \$node;
}
// Display Reversal view of linked list using recursion
public	function printReverse(\$node)
{
if (\$node == NULL)
{
return;
}
// Visit to next node
\$this->printReverse(\$node->next);
// Display node value
echo(" ".\$node->data);
}
}

function main()
{
\$sll = new SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}
main();``````

#### Output

`` 6 9 4 8 2 1``
``````// Node JS Program
// Print reverse of a linked list without actually reversing

{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display Reversal view of linked list using recursion
printReverse(node)
{
if (node == null)
{
return;
}
// Visit to next node
this.printReverse(node.next);
// Display node value
process.stdout.write(" " + node.data);
}
}

function main()
{
var sll = new SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}
main();``````

#### Output

`` 6 9 4 8 2 1``
``````#  Python 3 Program
#  Print reverse of a linked list without actually reversing

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :
def __init__(self) :
self.tail = None

else :
#  Append the node at last position
self.tail.next = node

self.tail = node

#  Display Reversal view of linked list using recursion
def printReverse(self, node) :
if (node == None) :
return

#  Visit to next node
self.printReverse(node.next)
#  Display node value
print(" ", node.data, end = "")

def main() :
sll = SingleLL()
#   1 → 2 → 8 → 4 → 9 → 6 → NULL
#  Reversal view
#  6 9 4 8 2 1

if __name__ == "__main__": main()``````

#### Output

``  6  9  4  8  2  1``
``````#  Ruby Program
#  Print reverse of a linked list without actually reversing

attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL
def initialize()
self.tail = nil
end

else

#  Append the node at last position
self.tail.next = node
end

self.tail = node
end

#  Display Reversal view of linked list using recursion
def printReverse(node)
if (node == nil)
return
end

#  Visit to next node
self.printReverse(node.next)
#  Display node value
print(" ", node.data)
end

end

def main()
sll = SingleLL.new()
#   1 → 2 → 8 → 4 → 9 → 6 → NULL
#  Reversal view
#  6 9 4 8 2 1
end

main()``````

#### Output

`` 6 9 4 8 2 1``
``````// Scala Program
// Print reverse of a linked list without actually reversing

var data: Int,
{
def this(data: Int)
{
this(data,null);
}
}
class SingleLL(
{
def this()
{
this(null,null);
}
def addNode(data: Int): Unit = {
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display Reversal view of linked list using recursion
def printReverse(node: LinkNode): Unit = {
if (node == null)
{
return;
}
// Visit to next node
this.printReverse(node.next);
// Display node value
print(" " + node.data);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}
}``````

#### Output

`` 6 9 4 8 2 1``
``````// Swift 4 Program
// Print reverse of a linked list without actually reversing

{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
init()
{
self.tail = nil;
}
{
{
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
// Display Reversal view of linked list using recursion
{
if (node == nil)
{
return;
}
// Visit to next node
self.printReverse(node!.next);
// Display node value
print(" ", node!.data, terminator: "");
}
}
func main()
{
let sll: SingleLL = SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}
main();``````

#### Output

``  6  9  4  8  2  1``
``````// Kotlin Program
// Print reverse of a linked list without actually reversing

{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
// Display Reversal view of linked list using recursion
fun printReverse(node: LinkNode ? ): Unit
{
if (node == null)
{
return;
}
// Visit to next node
this.printReverse(node.next);
// Display node value
print(" " + node.data);
}
}
fun main(args: Array < String > ): Unit
{
val sll: SingleLL = SingleLL();
//  1 → 2 → 8 → 4 → 9 → 6 → NULL
// Reversal view
// 6 9 4 8 2 1
}``````

#### Output

`` 6 9 4 8 2 1``

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.