Bird
Raised Fist0
C Sharp (C#)programming~20 mins

LinkedList usage in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
LinkedList Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LinkedList AddFirst and AddLast
What is the output of the following C# code?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<int> list = new LinkedList<int>();
        list.AddFirst(10);
        list.AddLast(20);
        list.AddFirst(5);
        foreach (var item in list) {
            Console.Write(item + " ");
        }
    }
}
A10 5 20
B20 10 5
C10 20 5
D5 10 20
Attempts:
2 left
💡 Hint
Remember AddFirst adds to the start, AddLast adds to the end.
Predict Output
intermediate
2:00remaining
LinkedList Remove and Count
What is the output of this C# code snippet?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<string> names = new LinkedList<string>();
        names.AddLast("Anna");
        names.AddLast("Bob");
        names.AddLast("Cara");
        names.Remove("Bob");
        Console.WriteLine(names.Count);
    }
}
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Removing an element decreases the count by one.
🔧 Debug
advanced
2:00remaining
Find the error in LinkedList node insertion
What error will this C# code produce when run?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<int> numbers = new LinkedList<int>();
        numbers.AddLast(1);
        numbers.AddLast(3);
        var node = numbers.Find(2);
        numbers.AddAfter(node, 2);
        foreach (var n in numbers) {
            Console.Write(n + " ");
        }
    }
}
ANullReferenceException
BInvalidOperationException
CNo error, output: 1 3 2
DArgumentNullException
Attempts:
2 left
💡 Hint
Check what happens if Find returns null and you use it as a node.
Predict Output
advanced
2:00remaining
LinkedList Reverse Traversal Output
What is the output of this C# code?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<char> letters = new LinkedList<char>();
        letters.AddLast('a');
        letters.AddLast('b');
        letters.AddLast('c');
        var node = letters.Last;
        while (node != null) {
            Console.Write(node.Value);
            node = node.Previous;
        }
    }
}
Aabc
Bcba
Cbac
Dcab
Attempts:
2 left
💡 Hint
Start from the last node and move backwards.
🧠 Conceptual
expert
2:00remaining
LinkedList Node Removal Impact
Given a LinkedList with elements 1, 2, 3, 4, 5, what is the value of the node after removing the node with value 3?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        LinkedList<int> list = new LinkedList<int>(new int[] {1, 2, 3, 4, 5});
        var node = list.Find(3);
        var nextNode = node.Next;
        list.Remove(node);
        Console.WriteLine(nextNode.Value);
    }
}
A3
B5
C4
DThrows InvalidOperationException
Attempts:
2 left
💡 Hint
Check what happens to the Next property of a node after removal.

Practice

(1/5)
1. What is a key characteristic of a LinkedList in C#?
easy
A. It stores elements in nodes linked by references.
B. It stores elements in a fixed-size array.
C. It only allows adding elements at the end.
D. It cannot remove elements once added.

Solution

  1. Step 1: Understand LinkedList structure

    A LinkedList stores elements in nodes, where each node points to the next (and possibly previous) node.
  2. Step 2: Compare options with LinkedList behavior

    Only It stores elements in nodes linked by references. correctly describes this linked node structure; others describe arrays or incorrect behaviors.
  3. Final Answer:

    It stores elements in nodes linked by references. -> Option A
  4. Quick Check:

    LinkedList = nodes linked by references [OK]
Hint: LinkedList uses nodes connected by links, not arrays. [OK]
Common Mistakes:
  • Thinking LinkedList uses arrays internally
  • Assuming LinkedList only adds at the end
  • Believing LinkedList cannot remove elements
2. Which of the following is the correct way to add an element at the start of a LinkedList<int> named list?
easy
A. list.AddStart(10);
B. list.AddFirst(10);
C. list.InsertAt(0, 10);
D. list.PushFront(10);

Solution

  1. Step 1: Recall LinkedList method names

    The method to add an element at the start is AddFirst.
  2. Step 2: Check each option's validity

    Only AddFirst is a valid LinkedList method; others are invalid or do not exist.
  3. Final Answer:

    list.AddFirst(10); -> Option B
  4. Quick Check:

    AddFirst adds at start [OK]
Hint: Use AddFirst to add at the start of LinkedList. [OK]
Common Mistakes:
  • Using non-existent methods like AddStart or PushFront
  • Confusing LinkedList with List methods
  • Trying to use InsertAt which LinkedList does not have
3. What will be the output of this C# code?
var list = new LinkedList<string>();
list.AddLast("apple");
list.AddFirst("banana");
list.AddLast("cherry");
foreach(var item in list) Console.Write(item + " ");
medium
A. banana apple cherry
B. apple banana cherry
C. cherry apple banana
D. banana cherry apple

Solution

  1. Step 1: Track insertion order

    First, "apple" is added last, so list: apple. Then "banana" added first, so list: banana, apple. Then "cherry" added last, so list: banana, apple, cherry.
  2. Step 2: Understand foreach iteration order

    Foreach iterates from first to last node, so output is "banana apple cherry ".
  3. Final Answer:

    banana apple cherry -> Option A
  4. Quick Check:

    First = banana, last = cherry [OK]
Hint: AddFirst puts item at start; AddLast at end. [OK]
Common Mistakes:
  • Assuming AddLast adds at start
  • Confusing order of AddFirst and AddLast
  • Expecting output in reverse order
4. Identify the error in this code snippet:
var list = new LinkedList<int>();
list.AddFirst(1);
list.AddLast(2);
list.Remove(3);
Console.WriteLine(list.Count);
medium
A. Remove(3) throws an exception because 3 is not in the list.
B. Count property does not exist on LinkedList.
C. AddFirst and AddLast methods are invalid for LinkedList.
D. Remove(3) does nothing since 3 is not found; Count remains 2.

Solution

  1. Step 1: Understand Remove behavior

    Remove(value) tries to remove the first node with that value. If not found, it does nothing and returns false; no exception is thrown.
  2. Step 2: Check Count after removal attempt

    Since 3 is not in the list, list remains with 2 elements; Count is 2.
  3. Final Answer:

    Remove(3) does nothing since 3 is not found; Count remains 2. -> Option D
  4. Quick Check:

    Remove missing value = no error, Count unchanged [OK]
Hint: Remove missing item does not throw error, just returns false. [OK]
Common Mistakes:
  • Expecting Remove to throw exception if item missing
  • Thinking AddFirst/AddLast are invalid
  • Assuming Count is not a property
5. Given a LinkedList<int> with values 1, 2, 3, 4, 5, which code snippet correctly removes all even numbers from the list?
hard
A. foreach(var node in list) { if(node % 2 == 0) list.Remove(node); }
B. for(int i = 0; i < list.Count; i++) { if(list.ElementAt(i) % 2 == 0) list.Remove(list.ElementAt(i)); }
C. var current = list.First; while(current != null) { var next = current.Next; if(current.Value % 2 == 0) list.Remove(current); current = next; }
D. list.RemoveAll(x => x % 2 == 0);

Solution

  1. Step 1: Understand safe removal during iteration

    Removing nodes while iterating requires storing next node before removal to avoid invalid references.
  2. Step 2: Analyze each option

    foreach(var node in list) { if(node % 2 == 0) list.Remove(node); } uses foreach which throws error on modification during iteration. var current = list.First; while(current != null) { var next = current.Next; if(current.Value % 2 == 0) list.Remove(current); current = next; } correctly uses a while loop with next node saved. for(int i = 0; i < list.Count; i++) { if(list.ElementAt(i) % 2 == 0) list.Remove(list.ElementAt(i)); } uses ElementAt which is inefficient and unsafe. list.RemoveAll(x => x % 2 == 0); is invalid as LinkedList has no RemoveAll method.
  3. Final Answer:

    var current = list.First; while(current != null) { var next = current.Next; if(current.Value % 2 == 0) list.Remove(current); current = next; } -> Option C
  4. Quick Check:

    Use while loop with next saved to remove nodes safely [OK]
Hint: Save next node before removal to avoid iteration errors. [OK]
Common Mistakes:
  • Modifying list inside foreach causes runtime error
  • Using RemoveAll which LinkedList does not have
  • Using ElementAt which is inefficient and unsafe