Bird
Raised Fist0
DSA C#programming~20 mins

Creating a Singly Linked List from Scratch in DSA C# - Debugging & Practice

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Adding Nodes to a Singly Linked List
What is the printed state of the linked list after adding three nodes with values 10, 20, and 30 in order?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) {
        Value = value;
        Next = null;
    }
}

public class SinglyLinkedList {
    public Node Head;
    public void Add(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = newNode;
        } else {
            Node current = Head;
            while (current.Next != null) {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }
    public void Print() {
        Node current = Head;
        while (current != null) {
            System.Console.Write(current.Value + " -> ");
            current = current.Next;
        }
        System.Console.WriteLine("null");
    }
}

var list = new SinglyLinkedList();
list.Add(10);
list.Add(20);
list.Add(30);
list.Print();
A30 -> 20 -> 10 -> null
B10 -> 30 -> 20 -> null
C10 -> 20 -> 30 -> null
Dnull
Attempts:
2 left
💡 Hint
Remember nodes are added at the end, so the order is the same as insertion.
❓ Predict Output
intermediate
2:00remaining
Output After Removing the Head Node
What is the printed state of the linked list after removing the head node from a list containing 5 -> 15 -> 25 -> null?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) {
        Value = value;
        Next = null;
    }
}

public class SinglyLinkedList {
    public Node Head;
    public void Add(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = newNode;
        } else {
            Node current = Head;
            while (current.Next != null) {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }
    public void RemoveHead() {
        if (Head != null) {
            Head = Head.Next;
        }
    }
    public void Print() {
        Node current = Head;
        while (current != null) {
            System.Console.Write(current.Value + " -> ");
            current = current.Next;
        }
        System.Console.WriteLine("null");
    }
}

var list = new SinglyLinkedList();
list.Add(5);
list.Add(15);
list.Add(25);
list.RemoveHead();
list.Print();
Anull
B5 -> 15 -> 25 -> null
C25 -> null
D15 -> 25 -> null
Attempts:
2 left
💡 Hint
Removing the head means the second node becomes the new head.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Node Addition
What error will occur when running this code that tries to add nodes to a singly linked list?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) {
        Value = value;
        Next = null;
    }
}

public class SinglyLinkedList {
    public Node Head;
    public void Add(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = newNode;
        } else {
            Node current = Head;
            while (current.Next == null) {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }
}

var list = new SinglyLinkedList();
list.Add(1);
list.Add(2);
ANullReferenceException at while loop
BStackOverflowException due to infinite recursion
CNo error, list adds nodes correctly
DCompile-time error due to syntax
Attempts:
2 left
💡 Hint
Check the while loop condition carefully; it may cause a null pointer access.
📝 Syntax
advanced
2:00remaining
Syntax Error in Node Class Definition
Which option correctly fixes the syntax error in this Node class definition?
DSA C#
public class Node {
    public int Value
    public Node Next;
    public Node(int value) {
        Value = value;
        Next = null;
    }
}
AAdd a semicolon after 'public int Value;'
BChange 'public int Value' to 'int Value;' without public
CRemove the constructor
DAdd parentheses after 'public int Value()'
Attempts:
2 left
💡 Hint
In C#, each field declaration must end with a semicolon.
🚀 Application
expert
3:00remaining
Resulting List After Complex Operations
Given the following operations on an initially empty singly linked list, what is the printed state of the list? Operations: - Add 100 - Add 200 - Remove head - Add 300 - Add 400 - Remove head - Add 500 Print the list after these operations.
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) {
        Value = value;
        Next = null;
    }
}

public class SinglyLinkedList {
    public Node Head;
    public void Add(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = newNode;
        } else {
            Node current = Head;
            while (current.Next != null) {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }
    public void RemoveHead() {
        if (Head != null) {
            Head = Head.Next;
        }
    }
    public void Print() {
        Node current = Head;
        while (current != null) {
            System.Console.Write(current.Value + " -> ");
            current = current.Next;
        }
        System.Console.WriteLine("null");
    }
}

var list = new SinglyLinkedList();
list.Add(100);
list.Add(200);
list.RemoveHead();
list.Add(300);
list.Add(400);
list.RemoveHead();
list.Add(500);
list.Print();
A100 -> 200 -> 300 -> 400 -> 500 -> null
B300 -> 400 -> 500 -> null
C200 -> 300 -> 400 -> 500 -> null
D400 -> 500 -> null
Attempts:
2 left
💡 Hint
Track the head after each removal and additions carefully.

Practice

(1/5)
1. What is the main purpose of a Singly Linked List in data structures?
easy
A. To store elements in a linear order where each element points to the next
B. To store elements in a tree-like hierarchical structure
C. To store elements with direct access by index like an array
D. To store elements in a key-value pair format

Solution

  1. Step 1: Understand the structure of a singly linked list

    A singly linked list stores elements in nodes where each node points to the next node, forming a chain.
  2. Step 2: Compare with other data structures

    Unlike trees or arrays, singly linked lists do not have hierarchical or direct index access but maintain a linear sequence through pointers.
  3. Final Answer:

    To store elements in a linear order where each element points to the next -> Option A
  4. Quick Check:

    Singly Linked List = linear chain of nodes [OK]
Hint: Remember: singly linked list nodes point only forward [OK]
Common Mistakes:
  • Confusing linked lists with arrays having direct index access
  • Thinking linked lists store key-value pairs
  • Assuming linked lists have hierarchical structure like trees
2. Which of the following is the correct way to declare a node class for a singly linked list in C#?
easy
A. public class Node { public int data; public Node previous; }
B. public class Node { int data; int next; }
C. public class Node { public int data; public int next; }
D. public class Node { public int data; public Node next; }

Solution

  1. Step 1: Identify the correct node structure for singly linked list

    A node must store data and a reference to the next node, so the next pointer type should be Node.
  2. Step 2: Check each option's correctness

    public class Node { public int data; public Node next; } correctly declares data as int and next as Node. Options B and C incorrectly use int for next. public class Node { public int data; public Node previous; } uses previous which is for doubly linked lists.
  3. Final Answer:

    public class Node { public int data; public Node next; } -> Option D
  4. Quick Check:

    Node next pointer type = Node [OK]
Hint: Next pointer must be Node type, not int [OK]
Common Mistakes:
  • Using int type for next pointer instead of Node
  • Confusing singly linked list with doubly linked list (previous pointer)
  • Omitting access modifiers making members inaccessible
3. What will be the output of the following C# code that creates a singly linked list with nodes 10, 20, 30 and prints the list?
public class Node {
    public int data;
    public Node next;
    public Node(int d) { data = d; next = null; }
}

public class LinkedList {
    public Node head;
    public void Add(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }
    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        Console.WriteLine("null");
    }
}

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.Add(10);
        list.Add(20);
        list.Add(30);
        list.PrintList();
    }
}
medium
A. null
B. 10 -> 20 -> 30 -> null
C. 10 -> 20 -> null
D. 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Trace the Add method calls

    Adding 10 sets head to node with data 10. Adding 20 appends node with data 20 at end. Adding 30 appends node with data 30 at end.
  2. Step 2: Trace the PrintList method

    PrintList starts at head and prints each node's data followed by ' -> ' until null is reached, printing "10 -> 20 -> 30 -> null".
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option B
  4. Quick Check:

    Nodes added in order = printed in order [OK]
Hint: Add appends at end; print shows nodes in insertion order [OK]
Common Mistakes:
  • Assuming Add inserts at head instead of tail
  • Forgetting to print 'null' at end
  • Printing nodes in reverse order
4. Identify the error in the following C# code snippet for adding a node to a singly linked list:
public void Add(int data) {
    Node newNode = new Node(data);
    if (head == null) {
        head = newNode;
    } else {
        Node temp = head;
        while (temp != null) {
            temp = temp.next;
        }
        temp.next = newNode;
    }
}
medium
A. The while loop condition should be 'temp.next != null' instead of 'temp != null'
B. The newNode should be created outside the method
C. The head should be set to null inside the else block
D. The method should return the newNode

Solution

  1. Step 1: Analyze the while loop condition

    The loop currently runs while temp != null, so it stops when temp is null, meaning temp.next is not accessible.
  2. Step 2: Correct the loop to stop at last node

    The loop should run while temp.next != null to stop at the last node, so we can set temp.next = newNode safely.
  3. Final Answer:

    The while loop condition should be 'temp.next != null' instead of 'temp != null' -> Option A
  4. Quick Check:

    Traverse until last node (temp.next != null) [OK]
Hint: Loop until temp.next is null to find last node [OK]
Common Mistakes:
  • Looping until temp is null causes null reference error
  • Trying to assign next on null reference
  • Misplacing node creation outside method
5. You want to create a method Reverse() in your singly linked list class that reverses the list in place. Which approach correctly reverses the list?
hard
A. Swap data values between head and tail nodes repeatedly until the middle is reached.
B. Create a new list and copy nodes from the original list in reverse order.
C. Iterate through the list, for each node set its next pointer to the previous node, updating previous and current pointers until the end.
D. Set head to null and re-add nodes in reverse order using Add method.

Solution

  1. Step 1: Understand in-place reversal logic

    Reversing a singly linked list in place requires changing each node's next pointer to point to the previous node, not creating new nodes or swapping data.
  2. Step 2: Evaluate options for correctness

    Iterating through the list, for each node setting its next pointer to the previous node while updating previous and current pointers until the end correctly describes the standard in-place reversal. The other options involve extra memory or inefficient operations.
  3. Final Answer:

    Iterate through the list, for each node set its next pointer to the previous node, updating previous and current pointers until the end. -> Option C
  4. Quick Check:

    Reverse by redirecting next pointers in place [OK]
Hint: Reverse by changing next pointers, not data or new nodes [OK]
Common Mistakes:
  • Creating new lists instead of reversing in place
  • Swapping data instead of pointers
  • Resetting head to null losing the list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard