Data-Structures
November 6, 2023
Question 1999 – KVS 22-12-2018 Part-B
November 6, 2023
Data-Structures
November 6, 2023
Question 1999 – KVS 22-12-2018 Part-B
November 6, 2023

Nielit Scientist-B CS 22-07-2017

Question 1
What does the following functions do for a given Linked list with first node as head?
void fun1(Struct node* head)
{

if(head==NULL)
return;
fun1(head → next);
printf(“%d”.head → data);
}
A
Prints all nodes of linked lists
B
Prints all nodes of linked list in reverse order
C
Prints alternate nodes of Linked List
D
Prints alternate nodes in reverse order
Question 1 Explanation: 
Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes.
Input : Head of following linked list
1->2->3->4->NULL
Output : Linked list should be changed to,
4->3->2->1->NULL
Algorithm:
1. Initialize three pointers prev as NULL, curr as head and next as NULL.
2. Iterate through the linked list. In loop, do following.
// Before changing next of current,
// store next node
next = curr->next
// Now change next of current
// This is where actual reversing happens
curr->next = prev
// Move prev and curr one step forward
prev = curr
curr = next
Correct Answer: B
Question 1 Explanation: 
Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes.
Input : Head of following linked list
1->2->3->4->NULL
Output : Linked list should be changed to,
4->3->2->1->NULL
Algorithm:
1. Initialize three pointers prev as NULL, curr as head and next as NULL.
2. Iterate through the linked list. In loop, do following.
// Before changing next of current,
// store next node
next = curr->next
// Now change next of current
// This is where actual reversing happens
curr->next = prev
// Move prev and curr one step forward
prev = curr
curr = next

Leave a Reply

Your email address will not be published. Required fields are marked *