PadhaikoAll subjects

Past exam question: 2066 Jestha (Back), Q3

Data Structure and Algorithms · 8 marks

Open in the Padhaiko app

Topic: Linked List

Question

What are the types of linked list? Discuss the consideration that has to be taken while developing algorithm/program with a linked list. Why alias variables are dangerous in a linked list? Write an algorithm to delete the first node in a singly linked list.

Solution

Types of Linked List & Design Considerations:

Why Alias Variables Are Dangerous in a Linked List: An alias occurs when two pointer variables (e.g., p and q) point to the exact same node in memory. If one pointer frees the node (free(p)) or alters its next field, the other pointer q becomes a dangling pointer—dereferencing q later causes undefined behavior, corrupt data, or crashes (segmentation faults), and freeing q causes a double-free error.

Algorithm to Delete the First Node in a Singly Linked List:

Algorithm DELETE_FIRST(head):
1. If head == NULL:
       Print "List Underflow"
       Return NULL
2. temp = head
3. head = head->next
4. Free(temp)
5. Return head
Answer:

Linked lists include singly, doubly, and circular variants. Safe code checks NULL boundaries, frees removed nodes, and preserves links before overwriting. Aliases risk dangling pointers and double-frees when two pointers share one node. DELETE_FIRST sets temp = head; head = head->next; Free(temp).

All past exam questions for Data Structure and Algorithms