AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumlinked listdoubly

~60s

Pointer updates in a doubly linked list

A new node is inserted between two existing nodes of a doubly linked list. How many pointer fields have to be assigned?

Show the answer and the worked solution

Correct answer: D.

4

Worked solution

  1. The new node needs two links of its own: one to its predecessor and one to its successor.
  2. The predecessor's next pointer must be redirected to the new node.
  3. The successor's prev pointer must be redirected to the new node.
  4. 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.

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.

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. 1
  2. 2
  3. 3
  4. 4
Show the answer and the worked solution

Correct answer: D. 4

Worked solution

  1. The new node needs two links of its own: one to its predecessor and one to its successor.
  2. The predecessor's next pointer must be redirected to the new node.
  3. The successor's prev pointer must be redirected to the new node.
  4. 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