📖 Scenario: Imagine you are managing a line of people waiting to buy tickets. Each person is represented as a node in a linked list. When a new person arrives, they join at the end of the line.
🎯 Goal: You will build a simple linked list and write code to insert a new node at the end (tail) of the list. Finally, you will print the list to see the order of people.
📋 What You'll Learn
Create a linked list with three nodes having values 10, 20, and 30
Create a variable called new_node with value 40
Write code to insert new_node at the end of the linked list
Print the linked list values in order separated by ' -> ' and ending with ' -> None'
💡 Why This Matters
🌍 Real World
Linked lists are used in real-world applications like managing playlists, undo functionality in apps, and handling queues where elements are added or removed dynamically.
💼 Career
Understanding linked lists and how to insert nodes is fundamental for software developers, especially when working with data structures that require dynamic memory management.
Progress0 / 4 steps
1
Create the initial linked list
Create a class called Node with attributes data and next. Then create three nodes with values 10, 20, and 30. Link them so that head points to the first node, which links to the second, which links to the third.
DSA Python
Hint
Remember to link each node's next to the following node.
2
Create the new node to insert
Create a new node called new_node with value 40.
DSA Python
Hint
Use the Node class to create new_node with data 40.
3
Insert the new node at the end
Write code to insert new_node at the end of the linked list starting from head. Use a variable current to traverse the list until the last node, then set its next to new_node.
DSA Python
Hint
Use a while loop to find the last node where next is None.
4
Print the linked list
Write code to print the linked list starting from head. Print each node's data followed by -> . End the print with None.
DSA Python
Hint
Use a while loop to print each node's data followed by ' -> '. After the loop, print 'None'.
Practice
(1/5)
1. What does the "Insert at End (Tail Insert)" operation do in a singly linked list?
easy
A. Adds a new node at the end of the list
B. Removes the last node from the list
C. Adds a new node at the beginning of the list
D. Deletes the first node of the list
Solution
Step 1: Understand the operation
Insert at End means adding a new node after the last node in the list.
Step 2: Compare with other operations
Other options describe removing or adding at the start, which is different.
Final Answer:
Adds a new node at the end of the list -> Option A
Quick Check:
Insert at End = Add node at end [OK]
Hint: Tail insert always adds node after the last one [OK]
Common Mistakes:
Confusing insert at end with insert at beginning
Thinking it removes nodes instead of adding
Mixing up head and tail operations
2. Which of the following is the correct syntax to create a new node and insert it at the end of a singly linked list in Python?
easy
A. new_node = Node(data); new_node.next = head; head = new_node
B. new_node = Node(data); tail = new_node; new_node.next = tail
C. new_node = Node(data); head.next = new_node; head = new_node
D. new_node = Node(data); tail.next = new_node; tail = new_node
Solution
Step 1: Create new node and link it after tail
We create new_node and set tail.next to it, then update tail to new_node.
Step 2: Check other options for correctness
new_node = Node(data); new_node.next = head; head = new_node inserts at head, C wrongly updates head, D sets tail before linking.
Hint: Tail insert updates tail.next then tail pointer [OK]
Common Mistakes:
Assigning new_node.next before linking to tail
Updating head instead of tail
Not updating tail pointer after insertion
3. What will be the output linked list after executing this code?
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(1)
tail = head
for value in [2, 3, 4]:
new_node = Node(value)
tail.next = new_node
tail = new_node
# Print list
current = head
result = []
while current:
result.append(current.data)
current = current.next
print(result)
medium
A. [1, 2, 3, 4]
B. [4, 3, 2, 1]
C. [1, 2, 3]
D. [2, 3, 4]
Solution
Step 1: Build linked list by tail insertion
Start with head=1, then add 2, 3, 4 at tail in order.
Step 2: Traverse and collect node data
Traverse from head, collect [1, 2, 3, 4] in order.
Final Answer:
[1, 2, 3, 4] -> Option A
Quick Check:
Tail insert preserves order [OK]
Hint: Tail insert keeps order same as input list [OK]
Common Mistakes:
Confusing order and reversing list
Printing only last inserted nodes
Not updating tail pointer correctly
4. Identify the error in this tail insert code snippet:
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(10)
tail = head
new_node = Node(20)
new_node.next = tail
tail = new_node
medium
A. tail should be updated before assigning new_node.next
B. new_node.next should be None, not tail
C. head should be updated instead of tail
D. No error, code is correct
Solution
Step 1: Check linking of new_node
new_node.next points to tail, which creates a cycle or wrong link.
Step 2: Correct linking for tail insert
new_node.next should be None because it's new tail; tail.next should point to new_node.
Final Answer:
new_node.next should be None, not tail -> Option B
Quick Check:
New tail's next = None [OK]
Hint: New tail node's next must be None [OK]
Common Mistakes:
Linking new_node.next to old tail
Not updating tail.next to new_node
Confusing head and tail pointers
5. Given a singly linked list with nodes containing values [5, 10, 15], write the sequence of steps to insert a new node with value 20 at the end, and then print the list. What will be the final printed output?
hard
A. [5, 10, 15]
B. [20, 5, 10, 15]
C. [5, 10, 15, 20]
D. [5, 10, 20, 15]
Solution
Step 1: Traverse to the last node (value 15)
Start at head (5), move to 10, then 15 which is tail (next is None).
Step 2: Create new node with value 20 and link it
Set last node's next to new node (20), new node's next is None.
Step 3: Traverse and print all nodes
Traverse from head and collect values: 5, 10, 15, 20.
Final Answer:
[5, 10, 15, 20] -> Option C
Quick Check:
Tail insert adds node at end [OK]
Hint: Traverse to tail, link new node, print full list [OK]
Common Mistakes:
Inserting new node in middle instead of end
Not setting new node's next to None
Forgetting to update last node's next pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.