What is the output of following function in which start is pointing to the first node of the following linked list 1->2->3->4->5->6 ?
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node() { data = 0; next = nullptr; }
};
void fun(Node* start) {
if (start == nullptr)
return;
cout << start->data << " ";
if (start->next != nullptr)
fun(start->next->next);
cout << start->data << " ";
}
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
void fun(Node start) {
if (start == null)
return;
System.out.print(start.data + " ");
if (start.next != null)
fun(start.next.next);
System.out.print(start.data + " ");
}
class Node:
def __init__(self, data):
self.data = data
self.next = None
def fun(start):
if start is None:
return
print(start.data, end=' ')
if start.next is not None:
fun(start.next.next)
print(start.data, end=' ')
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function fun(start) {
if (start === null)
return;
console.log(start.data + ' ');
if (start.next !== null)
fun(start.next.next);
console.log(start.data + ' ');
}
1 4 6 6 4 1
1 3 5 1 3 5
1 2 3 5
1 3 5 5 3 1
This question is part of this quiz :
Top MCQs on Linked List Data Structure with Answers