Bird
Raised Fist0
DSA C#programming~10 mins

Creating a Singly Linked List from Scratch in DSA C# - Visual Walkthrough

Choose your learning style10 modes available
Concept Flow - Creating a Singly Linked List from Scratch
Start: Empty List
↓
Create new node with value
↓
Is list empty?
↓
Set head
↓
Set last node.next = new node
↓
Done
This flow shows how we create a new node and add it to the singly linked list, either as the first node or at the end.
Execution Sample
DSA C#
class Node {
  public int data;
  public Node next;
  public Node(int val) { data = val; next = null; }
}

Node head = null;

// Add 10
Node newNode = new Node(10);
if (head == null) head = newNode;
This code creates a node with value 10 and sets it as the head of the list if the list is empty.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with empty listNonehead = nullnull
2Create new node with value 10Node(10)newNode -> Node(10)null
3Check if list is empty (head == null)Node(10)head == null (True)null
4Set head = newNodeNode(10)head -> Node(10)10 -> null
5Create new node with value 20Node(10), Node(20)newNode -> Node(20)10 -> null
6Check if list is empty (head == null)Node(10), Node(20)head != null (False)10 -> null
7Traverse to last node (Node(10))Node(10), Node(20)current -> Node(10)10 -> null
8Set last node.next = newNodeNode(10), Node(20)Node(10).next -> Node(20)10 -> 20 -> null
9Create new node with value 30Node(10), Node(20), Node(30)newNode -> Node(30)10 -> 20 -> null
10Check if list is empty (head == null)Node(10), Node(20), Node(30)head != null (False)10 -> 20 -> null
11Traverse to last node (Node(20))Node(10), Node(20), Node(30)current -> Node(20)10 -> 20 -> null
12Set last node.next = newNodeNode(10), Node(20), Node(30)Node(20).next -> Node(30)10 -> 20 -> 30 -> null
13End of insertionsNode(10), Node(20), Node(30)No pointer changes10 -> 20 -> 30 -> null
💡 All nodes inserted; list now has 3 nodes linked in order.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 8After Step 9After Step 12Final
headnullnullNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullNode(10)Node(10)Node(20)Node(20)Node(30)Node(30)Node(30)
currentnullnullnullnullNode(10)nullNode(20)null
Key Moments - 3 Insights
Why do we check if head is null before adding a new node?
Because if head is null, the list is empty and the new node becomes the first node (head). This is shown in execution_table step 3 and 4.
How do we find where to add the new node when the list is not empty?
We start at head and move through nodes until we find the last node whose next is null. This traversal is shown in steps 7 and 11.
What changes in the list when we add a new node at the end?
The last node's next pointer changes to point to the new node, linking it to the list. See pointer changes in steps 8 and 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the visual state of the list?
A20 -> null
Bnull
C10 -> null
D10 -> 20 -> null
💡 Hint
Check the Visual State column at step 4 in execution_table.
At which step does the list first have two nodes linked?
AStep 8
BStep 5
CStep 12
DStep 4
💡 Hint
Look for when Node(10).next points to Node(20) in Pointer Changes.
If we skip the traversal step before adding a new node, what happens?
AThe new node becomes head
BThe new node is not linked to the list
CThe last node's next pointer is updated correctly
DThe list becomes empty
💡 Hint
Refer to steps 6-8 where traversal is needed to link the new node.
Concept Snapshot
Creating a Singly Linked List:
- Start with head = null (empty list)
- Create a new node with value
- If list empty, set head = new node
- Else, traverse to last node
- Set last node.next = new node
- List nodes link forward only
- Visual: 10 -> 20 -> 30 -> null
Full Transcript
We start with an empty singly linked list where head is null. When we create a new node, if the list is empty, we set head to this new node. If not empty, we move through the list from head until we find the last node whose next pointer is null. We then set this last node's next pointer to the new node, adding it at the end. This process repeats for each new node. The list grows by linking nodes forward, ending with a null pointer. The visual state after adding nodes shows the chain of values connected by arrows ending in null.

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