Bird
Raised Fist0
DSA C#programming~10 mins

Creating a Singly Linked List from Scratch in DSA C# - Code Step by Step

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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;
    }
}
Drag options to blanks, or click blank then click option'
AnodeData
Bvalue
Cdata
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different field name than the constructor parameter.
Forgetting to declare the field as public.
2fill in blank
medium

Complete the code to initialize the head of the linked list to null.

DSA C#
public class SinglyLinkedList {
    private Node [1];

    public SinglyLinkedList() {
        head = null;
    }
}
Drag options to blanks, or click blank then click option'
Astart
Bhead
Ctail
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tail' instead of 'head' for the start of the list.
Not initializing the head to null.
3fill in blank
hard

Fix 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;
}
Drag options to blanks, or click blank then click option'
Ahead
Bhead.next
CnewNode
Dnull
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.
4fill in blank
hard

Fill 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");
}
Drag options to blanks, or click blank then click option'
A!=
B==
Ccurrent.next
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the while condition.
Assigning current to head inside the loop causes infinite loop.
5fill in blank
hard

Fill 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;
}
Drag options to blanks, or click blank then click option'
AnewNode
Bnext
Dhead
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.

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