Pointer updates in a doubly linked list — Aptitude Question
Pointer updates in a doubly linked list: A new node is inserted between two existing nodes of a doubly linked list. Answer and worked solution on CodeKairo.
- Topic: Data Structures & Algorithms
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 60 seconds
Question
A new node is inserted between two existing nodes of a doubly linked list. How many pointer fields have to be assigned?
Options
- 1
- 2
- 3
- 4
Show the answer and the worked solution
Correct answer: D. 4
Worked solution
- The new node needs two links of its own: one to its predecessor and one to its successor.
- The predecessor's
nextpointer must be redirected to the new node. - The successor's
prevpointer must be redirected to the new node. - That is 2 + 1 + 1 = 4 pointer assignments in total.
Answer: 4.
The idea
Every doubly linked splice touches four fields on insertion and two on deletion — a favourite way of separating candidates who have written the code from those who have only read about it.
Shortcut. Count the arrows touching the new node: two leaving it (prev and next) and two entering it (the predecessor's next and the successor's prev), which makes 4.
Common trap. 2 carries over the singly linked count, forgetting that the new node's prev and the successor's prev must also be set.
All Data Structures & Algorithms questions · placement mock tests