Assume the structure of a Linked List node is as follows.
C++
struct Node
{
int data;
struct Node *next;
};
|
C
struct Node
{
int data;
struct Node *next;
};
|
Java
static class Node
{
int data;
Node next;
};
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
|
C#
public class Node
{
public int data;
public Node next;
};
|
Javascript
<script>
class Node
{
constructor(item)
{
this .data = item;
this .next = null ;
}
}
</script>
|
Explain the functionality of the following C functions.
1. What does the following function do for a given Linked List?
C++14
void fun1( struct Node* head)
{
if (head == NULL)
return ;
fun1(head->next);
cout << head->data << " " ;
}
|
C
void fun1( struct Node* head)
{
if (head == NULL)
return ;
fun1(head->next);
printf ( "%d " , head->data);
}
|
Java
static void fun1(Node head)
{
if (head == null )
{
return ;
}
fun1(head.next);
System.out.print(head.data + " " );
}
|
Python
def fun1(head):
if (head = = None ):
return
fun1(head. next )
print (head.data, end = " " )
|
C#
static void fun1(Node head)
{
if (head == null )
{
return ;
}
fun1(head.next);
Console.Write(head.data + " " );
}
|
Javascript
<script>
function fun1( head)
{
if (head == null )
return ;
fun1(head.next);
document.write(head.data);
}
</script>
|
fun1() prints the given Linked List in the reverse way. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.
2. What does the following function do for a given Linked List?
C++
void fun2( struct Node* head)
{
if (head == NULL)
return ;
cout << head->data << " " ;
if (head->next != NULL )
fun2(head->next->next);
cout << head->data << " " ;
}
|
C
void fun2( struct Node* head)
{
if (head == NULL)
return ;
printf ( "%d " , head->data);
if (head->next != NULL )
fun2(head->next->next);
printf ( "%d " , head->data);
}
|
Java
static void fun2(Node head)
{
if (head == null )
{
return ;
}
System.out.print(head.data + " " );
if (head.next != null )
{
fun2(head.next.next);
}
System.out.print(head.data + " " );
}
|
Python3
def fun2(head):
if (head = = None ):
return
print (head.data, end = " " )
if (head. next ! = None ):
fun2(head. next . next )
print (head.data, end = " " )
|
C#
static void fun2(Node head)
{
if (head == null )
{
return ;
}
Console.Write(head.data + " " );
if (head.next != null )
{
fun2(head.next.next);
}
Console.Write(head.data + " " );
}
|
Javascript
<script>
function fun2( head)
{
if (head == null )
return ;
document.write(head.data);
if (head.next != null )
fun2(head.next.next);
document.write(head.data);
}
</script>
|
fun2() prints alternate nodes of the given Linked List, first from head to end, and then from end to head. If Linked List has even number of nodes, then fun2() skips the last node. For Linked List 1->2->3->4->5, fun2() prints 1 3 5 5 3 1. For Linked List 1->2->3->4->5->6, fun2() prints 1 3 5 5 3 1.
Below is a complete running program to test the above functions.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node *next;
};
void fun1(Node* head)
{
if (head == NULL)
return ;
fun1(head->next);
cout << head->data << " " ;
}
void fun2(Node* start)
{
if (start == NULL)
return ;
cout<<start->data<< " " ;
if (start->next != NULL )
fun2(start->next->next);
cout << start->data << " " ;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
cout<< "Output of fun1() for list 1->2->3->4->5 \n" ;
fun1(head);
cout<< "\nOutput of fun2() for list 1->2->3->4->5 \n" ;
fun2(head);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void fun1( struct Node* head)
{
if (head == NULL)
return ;
fun1(head->next);
printf ( "%d " , head->data);
}
void fun2( struct Node* start)
{
if (start == NULL)
return ;
printf ( "%d " , start->data);
if (start->next != NULL )
fun2(start->next->next);
printf ( "%d " , start->data);
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printf ( "Output of fun1() for list 1->2->3->4->5 \n" );
fun1(head);
printf ( "\nOutput of fun2() for list 1->2->3->4->5 \n" );
fun2(head);
getchar ();
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static void fun1(Node head)
{
if (head == null )
{
return ;
}
fun1(head.next);
System.out.print(head.data + " " );
}
static void fun2(Node start)
{
if (start == null )
{
return ;
}
System.out.print(start.data + " " );
if (start.next != null )
{
fun2(start.next.next);
}
System.out.print(start.data + " " );
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
System.out.print( "Output of fun1() for " +
"list 1->2->3->4->5 \n" );
fun1(head);
System.out.print( "\nOutput of fun2() for " +
"list 1->2->3->4->5 \n" );
fun2(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def fun1(head):
if (head = = None ):
return
fun1(head. next )
print (head.data, end = " " )
def fun2(start):
if (start = = None ):
return
print (start.data, end = " " )
if (start. next ! = None ):
fun2(start. next . next )
print (start.data, end = " " )
def push( head, new_data):
new_node = Node(new_data)
new_node. next = head
head = new_node
return head
head = None
head = Node( 5 )
head = push(head, 4 )
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 1 )
print ( "Output of fun1() for list 1->2->3->4->5" )
fun1(head)
print ( "\nOutput of fun2() for list 1->2->3->4->5" )
fun2(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static void fun1(Node head)
{
if (head == null )
{
return ;
}
fun1(head.next);
Console.Write(head.data + " " );
}
static void fun2(Node start)
{
if (start == null )
{
return ;
}
Console.Write(start.data + " " );
if (start.next != null )
{
fun2(start.next.next);
}
Console.Write(start.data + " " );
}
static Node Push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void Main(String[] args)
{
Node head = null ;
head = Push(head, 5);
head = Push(head, 4);
head = Push(head, 3);
head = Push(head, 2);
head = Push(head, 1);
Console.Write( "Output of fun1() for " +
"list 1->2->3->4->5 \n" );
fun1(head);
Console.Write( "\nOutput of fun2() for " +
"list 1->2->3->4->5 \n" );
fun2(head);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .next = null ;
this .data = data;
}
}
function fun1(head)
{
if (head == null )
{
return ;
}
fun1(head.next);
document.write(head.data + " " );
}
function fun2(start)
{
if (start == null )
{
return ;
}
document.write(start.data + " " );
if (start.next != null )
{
fun2(start.next.next);
}
document.write(start.data + " " );
}
function Push(head_ref, new_data)
{
let new_node = new Node(new_data);
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
let head = null ;
head = Push(head, 5);
head = Push(head, 4);
head = Push(head, 3);
head = Push(head, 2);
head = Push(head, 1);
document.write( "Output of fun1() for " +
"list 1->2->3->4->5 " + "</br>" );
fun1(head);
document.write( "</br>" );
document.write( "Output of fun2() for " +
"list 1->2->3->4->5 " + "</br>" );
fun2(head);
</script>
|
Output:
Output of fun1() for list 1->2->3->4->5
5 4 3 2 1
Output of fun2() for list 1->2->3->4->5
1 3 5 5 3 1
Time complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!