Lesson 1
Linked List Operations in C#
Lesson Overview

Welcome to our tutorial focusing on Linked List Operations in C#. Singly-Linked Lists (or just Linked Lists) are among the most fundamental data structures used in computer science. They provide an efficient way to store and access data that is not necessarily contiguous in memory. This ability distinguishes linked lists from arrays, making them an indispensable tool in a programmer's toolkit.

LinkedList Definition

To work with linked lists, we first need to define a ListNode class, which represents a node in the linked list.

C#
1class ListNode 2{ 3 public int Value; 4 public ListNode Next; 5 6 public ListNode(int value) 7 { 8 this.Value = value; // Holds the value or data of the node 9 this.Next = null; // Points to the next node in the linked list; default is null 10 } 11} 12 13// Initialization of linked list 14public class LinkedListExample 15{ 16 public static void Main(string[] args) 17 { 18 ListNode head = new ListNode(1); 19 head.Next = new ListNode(2); 20 head.Next.Next = new ListNode(3); 21 head.Next.Next.Next = new ListNode(4); 22 head.Next.Next.Next.Next = new ListNode(5); 23 } 24}

In the ListNode class:

  • Value holds the data of the node.
  • Next is a reference to the next node in the linked list. It is null by default, meaning the node does not point to any other node when it's freshly created.

To understand this, first know that a linked list is a linear data structure where each element is a separate object known as a node. A node comprises data and a reference (link) to the next node in the sequence.

The provided code creates a linked list where each node points to another as follows: 1 -> 2 -> 3 -> 4 -> 5, and the last node points to null.

Task Example

One example of a problem to practice involves reversing a linked list, a common operation in interviews and the industry. To reverse a linked list, we'll need to sequentially rearrange the Next link of each node to point toward its previous node.

Here is what the code might look like. Note that this code uses O(1)O(1) of additional memory:

C#
1class ListNode 2{ 3 public int Value; 4 public ListNode Next; 5 6 public ListNode(int value) 7 { 8 this.Value = value; 9 this.Next = null; 10 } 11} 12 13public class ReverseLinkedList 14{ 15 public static ListNode Reverse(ListNode head) 16 { 17 ListNode prev = null; 18 ListNode current = head; 19 while (current != null) 20 { 21 ListNode nextNode = current.Next; 22 current.Next = prev; 23 prev = current; 24 current = nextNode; 25 } 26 return prev; 27 } 28 29 // Test 30 public static void Main(string[] args) 31 { 32 ListNode head = new ListNode(1); 33 head.Next = new ListNode(2); 34 head.Next.Next = new ListNode(3); 35 head.Next.Next.Next = new ListNode(4); 36 head.Next.Next.Next.Next = new ListNode(5); 37 38 ListNode reversedHead = Reverse(head); 39 while (reversedHead != null) 40 { 41 System.Console.Write(reversedHead.Value + " "); 42 reversedHead = reversedHead.Next; // Output: 5 4 3 2 1 43 } 44 } 45}
Next: Practice!

I urge you to observe, analyze, and understand the problem and the corresponding solution. This practice will facilitate a well-rounded understanding of linked list operations and their applications. By the end of the tutorial, you should feel more comfortable tackling linked list problems, allowing you to handle similar tasks in technical interviews. Let's get started with practical exercises!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.