Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to traverse the linked list forward and print each node's value.
DSA C#
Node current = head; while (current [1] null) { Console.Write(current.Value + " -> "); current = current.Next; } Console.WriteLine("null");
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causes the loop to skip or never run.
Using comparison operators like '<' or '>' is invalid for objects.
✗ Incorrect
We use '!=' to continue the loop until current is null, meaning the end of the list.
2fill in blank
mediumComplete the code to traverse the linked list backward starting from tail and print each node's value.
DSA C#
Node current = tail; while (current [1] null) { Console.Write(current.Value + " -> "); current = current.Previous; } Console.WriteLine("null");
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causes the loop to skip or never run.
Using comparison operators like '<' or '>' is invalid for objects.
✗ Incorrect
We use '!=' to continue the loop until current is null, meaning the start of the list.
3fill in blank
hardFix the error in the code to correctly traverse forward and print node values.
DSA C#
Node current = head; while (current != null) { Console.Write(current.Value + " -> "); current = current.[1]; } Console.WriteLine("null");
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Previous' moves backward, causing incorrect traversal.
Using 'Head' or 'Tail' is invalid for moving between nodes.
✗ Incorrect
To move forward in the list, we must assign current to current.Next.
4fill in blank
hardFill both blanks to traverse backward and print node values starting from tail.
DSA C#
Node current = [1]; while (current != [2]) { Console.Write(current.Value + " -> "); current = current.Previous; } Console.WriteLine("null");
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from head instead of tail for backward traversal.
Stopping when current equals head instead of null.
✗ Incorrect
Start from tail and continue until current is null to traverse backward.
5fill in blank
hardFill all three blanks to create a dictionary mapping node values to their positions while traversing forward.
DSA C#
Dictionary<int, int> positions = new Dictionary<int, int>(); Node current = [1]; int index = 0; while (current != null) { positions[[2]] = index; current = current.[3]; index++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from tail instead of head.
Using current instead of current.Value as dictionary key.
Moving backward with Previous instead of forward with Next.
✗ Incorrect
Start from head, use current.Value as key, and move forward with current.Next.
