📖 Scenario: Imagine you want to keep a list of your favorite fruits in order. You want to add fruits one by one and see the list grow. A singly linked list is a simple way to do this, where each fruit points to the next one.
🎯 Goal: You will build a singly linked list from scratch in C#. You will create nodes, link them, and print the list to see all fruits in order.
📋 What You'll Learn
Create a Node class with a string data field and a Node reference to the next node
Create a LinkedList class with a head node
Add a method to insert new nodes at the end of the list
Add a method to print all nodes in the list in order
💡 Why This Matters
🌍 Real World
Singly linked lists are used in many software systems to manage ordered data where insertion and deletion happen frequently, like playlists, undo history, or navigation paths.
💼 Career
Understanding linked lists is fundamental for software developers and helps in solving problems related to dynamic data storage and manipulation.
Progress0 / 4 steps
1
Create the Node class
Create a public class called Node with a public string field called Data and a public Node field called Next. Add a constructor that takes a string data parameter and sets Data to it and Next to null.
DSA C#
Hint
Think of each Node as a box holding a fruit name and a pointer to the next box.
2
Create the LinkedList class with head
Create a public class called LinkedList with a public Node field called Head. Initialize Head to null in the constructor.
DSA C#
Hint
The LinkedList keeps track of the first node called Head.
3
Add method to insert nodes at the end
Inside the LinkedList class, add a public method called InsertAtEnd that takes a string data parameter. Create a new Node with this data. If Head is null, set Head to the new node. Otherwise, loop through nodes starting from Head until you find the last node (where Next is null) and set its Next to the new node.
DSA C#
Hint
Think of walking through the list until you find the last fruit, then add the new fruit after it.
4
Add method to print the list and display output
Inside the LinkedList class, add a public method called PrintList that loops through nodes starting from Head and prints each Data followed by " -> ". After the last node, print "null". Then, in the Main method, create a LinkedList object, insert these fruits in order: "Apple", "Banana", "Cherry", and call PrintList.
DSA C#
Hint
Print each fruit followed by an arrow, then print "null" at the end to show the list ends.
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
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.
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.
Final Answer:
To store elements in a linear order where each element points to the next -> Option A
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
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.
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.
Final Answer:
public class Node { public int data; public Node next; } -> Option D
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
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.
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".
Final Answer:
10 -> 20 -> 30 -> null -> Option B
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
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.
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.
Final Answer:
The while loop condition should be 'temp.next != null' instead of 'temp != null' -> Option A
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
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.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.