Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the Node class with a data field.
DSA C#
public class Node { public int [1]; public Node next; public Node(int data) { this.data = data; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different field name than the constructor parameter.
Forgetting to declare the field as public.
✗ Incorrect
The field to store the integer value in the Node class is commonly named 'data'.
2fill in blank
mediumComplete the code to initialize the head of the linked list to null.
DSA C#
public class SinglyLinkedList { private Node [1]; public SinglyLinkedList() { head = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tail' instead of 'head' for the start of the list.
Not initializing the head to null.
✗ Incorrect
The head variable points to the first node in the linked list and is initialized to null.
3fill in blank
hardFix the error in the AddFirst method to correctly add a new node at the start.
DSA C#
public void AddFirst(int data) {
Node newNode = new Node(data);
newNode.next = [1];
head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null loses the rest of the list.
Setting newNode.next to head.next skips the first node.
✗ Incorrect
The new node's next should point to the current head before updating head to newNode.
4fill in blank
hardFill both blanks to complete the method that prints all node values in the list.
DSA C#
public void PrintList() {
Node current = head;
while (current [1] null) {
Console.Write(current.data + " -> ");
current = [2];
}
Console.WriteLine("null");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the while condition.
Assigning current to head inside the loop causes infinite loop.
✗ Incorrect
The loop continues while current is not null, and current moves to current.next each step.
5fill in blank
hardFill all three blanks to complete the method that adds a new node at the end of the list.
DSA C#
public void AddLast(int data) {
Node newNode = new Node(data);
if (head == null) {
head = [1];
return;
}
Node current = head;
while (current.[2] != null) {
current = current.[3];
}
current.next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to null instead of newNode when list is empty.
Using current.next.next instead of current.next in the loop.
✗ Incorrect
If the list is empty, head points to newNode. Then traverse using current.next until the last node, then set current.next to newNode.
