A node is a basic unit that holds data and references (pointers) to other nodes, helping to build structures like linked lists and trees.
Click to reveal answer
beginner
What does a pointer in a node represent?
A pointer is a reference inside a node that points to another node, showing the connection or link between nodes.
Click to reveal answer
intermediate
Explain the difference between single and double pointers in node design.
Single pointer nodes link to the next node only (like in singly linked lists). Double pointer nodes have two references: one to the next node and one to the previous node (like in doubly linked lists).
Click to reveal answer
beginner
Why is it important to initialize pointers in a node?
Initializing pointers prevents errors by ensuring they either point to a valid node or null, avoiding unexpected behavior or crashes.
Click to reveal answer
beginner
Show a simple TypeScript node structure for a singly linked list.
C. The type of next should be 'Node | null' to allow null assignment
D. Class should not have a constructor
Solution
Step 1: Analyze the type of next pointer
The next property is typed as Node but assigned null, which is invalid.
Step 2: Correct the type to allow null
Change the type to Node | null so it can hold either a node or null.
Final Answer:
The type of next should be 'Node | null' to allow null assignment -> Option C
Quick Check:
Allow null in next type = The type of next should be 'Node | null' to allow null assignment [OK]
Hint: Next pointer must allow null for list end [OK]
Common Mistakes:
Not allowing null in next type
Removing constructor unnecessarily
Making data optional without reason
5. You want to design a doubly linked list node in TypeScript that stores a number and has pointers to both previous and next nodes. Which of the following class definitions correctly implements this with proper pointer types and initialization?