📖 Scenario: You are working on a simple contact list app. Each contact is stored as a node in a linked list. You want to find if a contact with a specific name exists in the list.
🎯 Goal: Build a linked list of contacts and write code to search for a contact by name.
📋 What You'll Learn
Create a linked list with exactly three nodes containing the names: "Alice", "Bob", and "Charlie" in that order.
Create a variable called searchName and set it to "Bob".
Write a loop to search the linked list for a node with the name equal to searchName.
Print "Found" if the name is found, otherwise print "Not Found".
💡 Why This Matters
🌍 Real World
Linked lists are used in many apps to store ordered data where you need to add or remove items efficiently, like contact lists or playlists.
💼 Career
Understanding linked lists and how to search them is a fundamental skill for software developers, especially for roles involving data structures and algorithms.
Progress0 / 4 steps
1
Create the linked list nodes
Create a class called Node with a public string field name and a public Node field next. Then create three nodes with names "Alice", "Bob", and "Charlie". Link them so that head points to the first node.
DSA C#
Hint
Define a Node class with name and next fields. Create nodes and link them by setting next.
2
Set the search name
Create a string variable called searchName and set it to "Bob".
DSA C#
Hint
Just create a string variable named searchName and assign it the value "Bob".
3
Search the linked list
Write a while loop to go through the linked list starting from head. Use a variable current to track the node. Inside the loop, check if current.name equals searchName. If yes, set a boolean variable found to true and break the loop. If the loop ends without finding, found should be false. Initialize found to false before the loop.
DSA C#
Hint
Use a while loop with a current node starting at head. Check each node's name. Set found to true and break if matched.
4
Print the search result
Write a Console.WriteLine statement that prints "Found" if found is true, otherwise prints "Not Found".
DSA C#
Hint
Use Console.WriteLine with an if statement to print "Found" or "Not Found" based on the found variable.
Practice
(1/5)
1. What is the main purpose of searching for a value in a linked list?
easy
A. To add a new node at the end
B. To find if the value exists in the list
C. To delete the first node
D. To reverse the linked list
Solution
Step 1: Understand the search operation
Searching means checking each node to see if it contains the target value.
Step 2: Identify the goal of searching
The goal is to find whether the value exists anywhere in the linked list.
Final Answer:
To find if the value exists in the list -> Option B
Quick Check:
Search = Find value presence [OK]
Hint: Search means checking nodes for the value [OK]
Common Mistakes:
Confusing search with insertion or deletion
Thinking search changes the list
Assuming search returns the node position always
2. Which of the following is the correct C# syntax to start traversing a linked list from its head node named head?
easy
A. Node current => head;
B. Node current == head;
C. Node current = head;
D. Node current <- head;
Solution
Step 1: Understand variable assignment syntax in C#
To assign a reference, use single equals sign =.
Step 2: Check each option for correct assignment
Node current = head; uses =, which is correct. Others use invalid operators.
Final Answer:
Node current = head; -> Option C
Quick Check:
Assignment uses = in C# [OK]
Hint: Use single = for assignment in C# [OK]
Common Mistakes:
Using == instead of = for assignment
Using invalid operators like => or <-
Confusing assignment with comparison
3. What will be the output of the following C# code snippet?
class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
Node head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
Node current = head;
bool found = false;
while (current != null) {
if (current.data == 10) {
found = true;
break;
}
current = current.next;
}
Console.WriteLine(found);
medium
A. NullReferenceException
B. False
C. Compilation error
D. True
Solution
Step 1: Trace the linked list nodes and search loop
The list has nodes with data 5 -> 10 -> 15. The loop checks each node's data for 10.
Step 2: Check when the value 10 is found
At second node, data is 10, so found becomes true and loop breaks.
Final Answer:
True -> Option D
Quick Check:
Value 10 found = True [OK]
Hint: Stop loop when value found, print true [OK]
Common Mistakes:
Forgetting to break loop after finding value
Printing current.data instead of found
Null pointer errors if next not checked
4. Identify the error in the following code that searches for a value in a linked list:
Node current = head;
while (current.next != null) {
if (current.data == 20) {
Console.WriteLine("Found");
break;
}
current = current.next;
}
medium
A. It skips checking the last node
B. It causes infinite loop
C. It throws NullReferenceException immediately
D. It never enters the loop
Solution
Step 1: Analyze the loop condition
The loop runs while current.next != null, so it stops before last node.
Step 2: Understand impact on search
The last node's data is never checked, so search may miss the value if it's there.
Final Answer:
It skips checking the last node -> Option A
Quick Check:
Loop condition misses last node [OK]
Hint: Use current != null to check all nodes [OK]
Common Mistakes:
Using current.next != null instead of current != null
Assuming last node is checked automatically
Not handling empty list (head null)
5. Given a linked list where nodes may contain duplicate values, how can you modify the search to return the count of occurrences of a target value x instead of just checking presence?
hard
A. Traverse all nodes, increment count when node.data == x, then return count
B. Stop at first occurrence and return 1
C. Use recursion to find only the last occurrence
D. Delete nodes with value x and return total nodes left
Solution
Step 1: Understand the need to count all occurrences
Since duplicates exist, we must check every node to count all matches.
Step 2: Traverse entire list and count matches
Loop through each node, increment a counter when node.data equals x, then return the count.
Final Answer:
Traverse all nodes, increment count when node.data == x, then return count -> Option A
Quick Check:
Count all matches by full traversal [OK]
Hint: Count matches by checking every node [OK]
Common Mistakes:
Stopping search after first match
Using recursion incorrectly for counting
Modifying list during search unintentionally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.