DEV Community

Cover image for C# Linked Lists: Exploring Dummy Nodes, Pointers, and More | Data Structures
Andres Ramirez
Andres Ramirez

Posted on

C# Linked Lists: Exploring Dummy Nodes, Pointers, and More | Data Structures

Implementing a Linked List (LL) involves creating a linear data structure with interconnected nodes. Utilizing pointers, such as 'Head' and 'Tail,' involves variables pointing to the memory locations of the first and last elements in the LL, respectively.

An essential concept in Linked Lists (LL) is their dependence on references rather than indexes. Navigating to a specific point, such as D, entails methodically traversing the sequence A → B → C → D.

Linked List


🔄IMPLEMENTATION

This code initializes a linked list with a single node containing a specified value. The LinkedList class includes a private Node class with properties for value and next, and a constructor that creates a new node and sets both head and tail to reference it, initializing the length to 1.

Linked List Implementation


📝LINKED LISTS TYPES

  • Single LL: Each node in the list contains a reference (or pointer) to the next node in the sequence. The last node points to null, indicating the end of the list.
  • Doubly LL: Each node contains references to both the next and the previous nodes in the sequence. Allows traversal in both forward and backward directions.
  • Circular LL: Similar to a singly linked list, but the last node points back to the first node, forming a circular structure. Can be implemented with a singly linked list or a doubly linked list.

✏️BASIC OPERATIONS

To establish a robust foundation in Linked Lists, proficiency in the following key areas is imperative:

  • Get: Retrieve information from the Linked List.
  • Update: Modifying existing elements within the Linked List.
  • Insertion: Manage the addition of elements within the Linked List structure.
  • Deletion: Handle the removal of elements from the Linked List.
  • Reverse: Change the order of elements in the list so that the last node becomes the first and vice versa.

Get familiar with the Basic LL operations


📈FUNDAMENTALS FOR EXPLORING MORE COMPLEX LL EXERCISES

  • Linked Lists and Reference Types: In Linked Lists, nodes are reference types. This means that any operation affecting a node will also affect any pointer that points to it.
  • Pointer: A pointer is a variable that stores the memory address of another variable or object. In linked lists, pointers are used to keep track of the connections between nodes.
  • Dummy Node: A dummy node is a special node added at the beginning of a linked list to simplify certain operations. It does not hold meaningful data and is used as a placeholder.

CODING, CODING AND CODING...

The best way to illustrate these concepts is if we explore some exercises.

Linked List Cicle

Checkout this PROBLEM. We need to validate if a LL has cycle in it. This is a perfect example to illustrate the use of pointers.

Cycle in a LL

Code to solve this problem:
Ex1 Solution

Explanation:

  • Pointers (fast and slow) are essential for traversing a linked list, enabling movement from one node to the next. Without them, iterating through and examining nodes would be impossible.
  • By moving a fast pointer twice as fast as a slow pointer, the algorithm efficiently identifies whether a cycle exists in the list.

Remove Duplicates from Sorted List II

In this problem, we need to delete all nodes that have duplicate numbers. This is a perfect example to illustrate the use of a dummy node.

Removing Duplicate Elements in a LL

Code to solve this problem:
Ex2 Solution

It's important to note that we create a dummy node which always points to the head. This is convenient because even if the node pointed to by "head" changes, we'll always have a constant reference to the new value in question.

This case teaches us that WE CAN USE A DUMMY NODE IN SITUATIONS WHERE THE HEAD OR THE LINKED LIST ITSELF MAY UNDERGO CHANGES AS THEY GO THROUGH OUR CODE.

Merge Two Sorted Lists

In this problem we need to merge the two lists into one (new) sorted list. This is a very good example of using a dummy node, but with a different approach.

Merge Two Sorted Lists

Code to solve this problem:
Ex3 Solution

  • The dummy node acts as a placeholder for the merged list. It ensures that current always points to a valid node, even if the first node of the merged list changes during the merging process.
  • Inside the loop, the value of current is updated iteratively based on the values of the nodes in list1 and list2. By using a dummy node, we can avoid special handling for the first node of the merged list.
  • After the loop, the next pointer of the last node (current) points to the remaining nodes of either list1 or list2. The dummy node's next pointer (dummy.next) then serves as the head of the merged list.

CONCLUSION

Understanding pointers and dummy nodes is crucial for effectively solving complex problems involving linked lists. Pointers facilitate efficient traversal and manipulation of linked list elements, while dummy nodes provide a consistent starting point for various operations, simplifying code logic. Additionally, grasping the reference type nature of nodes in linked lists helps in managing node connections and ensuring proper list manipulation. Together, these concepts empower developers to tackle intricate linked list problems with clarity and efficiency.

Top comments (0)