📖 Scenario: Imagine you are building a simple linked list to keep track of tasks in a to-do list app. Each task is a node that holds the task name and a pointer to the next task.
🎯 Goal: You will create a node structure with a pointer to the next node, then link two nodes together, and finally print the linked list to see the tasks in order.
📋 What You'll Learn
Create a class called Node with two properties: task (string) and next (Node or null).
Create two nodes with tasks: 'Buy groceries' and 'Clean room'.
Link the first node's next pointer to the second node.
Print the tasks in the linked list starting from the first node.
💡 Why This Matters
🌍 Real World
Linked lists are used in many apps to manage ordered data like tasks, playlists, or history where you need to move through items one by one.
💼 Career
Understanding node structures and pointers is fundamental for software developers working with data structures, memory management, and building efficient algorithms.
Progress0 / 4 steps
1
Create the Node class
Create a class called Node with a constructor that takes a string parameter task and sets this.task to it. Also, add a property next initialized to null.
DSA Typescript
Hint
Use class keyword to create the Node class. The next property should be of type Node | null and start as null.
2
Create two nodes with tasks
Create two variables called firstNode and secondNode. Assign firstNode to a new Node with task 'Buy groceries' and secondNode to a new Node with task 'Clean room'.
DSA Typescript
Hint
Use the new keyword to create instances of the Node class with the exact task strings.
3
Link the first node to the second node
Set the next property of firstNode to point to secondNode.
DSA Typescript
Hint
Use dot notation to set firstNode.next equal to secondNode.
4
Print the linked list tasks
Write a while loop to start from firstNode and print each node's task property until the next pointer is null. Use console.log to print each task.
DSA Typescript
Hint
Use a variable to track the current node. Loop while it is not null. Print the task, then move to the next node.
Practice
(1/5)
1. What does the next pointer in a singly linked list node represent?
easy
A. It points to the previous node in the list
B. It stores the data value of the node
C. It holds the size of the list
D. It points to the next node in the list
Solution
Step 1: Understand the role of next in singly linked list
The next pointer is used to link the current node to the following node in the list.
Step 2: Differentiate from other pointers
Unlike prev which points backward, next always points forward to the next node.
Final Answer:
It points to the next node in the list -> Option D
Quick Check:
next pointer = next node [OK]
Hint: Remember: next means forward link in singly linked list [OK]
Common Mistakes:
Confusing next with previous pointer
Thinking next stores data instead of address
Assuming next holds list size
2. Which of the following is the correct TypeScript syntax to define a node class with a data property and a next pointer?
easy
A. class Node { data: number; next: Node; constructor(data: number) { this.data = data; this.next = null; } }
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?