Linked list
A linked list represents a set of linked nodes, each of which stores the data itself and a link to the next node. In real life, a linked list can be thought of as a train, each car of which can contain cargo or passengers and can also be linked to another car.

Thus, if in an array the position of elements is determined by indices, then in a linked list it is determined by pointers to the next and/or) previous element.
Linked lists can vary. There are singly linked lists, in which each node stores a pointer only to the next node. There are doubly linked lists, in which each element stores references to both the next and previous elements. There are also circular, closed lists. In this case, we will consider creating a singly linked list.
Before creating a list, we need to define a node class that will represent a single object in the list:
1 2 3 4 5 6 7 8 9 | public class Node<T> |
The Node class is generic, so it can store any data. The property is used to store data Data. The property is defined to reference the next node Next.
Next, we define the list class itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | using System.Collections;using System.Collections.Generic; public class LinkedList<T> : IEnumerable<T> // singly linked list{ Node<T>? head; // head/first element Node<T>? tail; // last/tail element int count; // number of elements in the list // adding an elementpublic void Add(T data) { Node<T> node = new Node<T>(data); if (head == null)head = node; else tail!.Next = node; tail = node;
|
Let’s go over the main points. List implementations may vary depending on the specific tasks, but all implementations share two key methods: adding and deleting.
But before performing various operations on the data, three variables are defined in the list class:
1 2 3 | Node<T>? head; // head/first element |
Adding method:
1 2 3 4 5 6 7 8 9 10 11 12 | public void Add(T data){ Node<T> node = new Node<T>(data); if (head == null)head = node; else tail!.Next = node; tail = node; count++;} |
If the head variable isn’t set (meaning the list is empty), then we set head and tail. After adding the first element, they will point to the same object.
If the list contains at least one element, we set the tail. Next property—it now stores a reference to the new node. We then reset tail—it now refers to the new node.
The complexity of this method is O(1). Graphically, it looks like this:

It’s important to note the presence of the tail variable, which points to the last element. Some implementations don’t use this variable and instead add it differently:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public void AddWithoutTail(T data){ Node<T> node = new Node<T>(data); if (head == null){ head = node; } else { Node<T> current = head; // find the last element while (current.Next != null) { current = current.Next; } // set the last element current.Next = node; } count++; } |
This method is quite effective and is often used, but the need to iterate through elements to find the last one increases the search time and the complexity of the algorithm. It is O(n) .
A special method is adding to the beginning of a list, where we simply reset the reference to the head element:
1 2 3 4 5 6 7 8 9 | public void AppendFirst(T data) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public bool Remove(T data){ Node<T>? current = head; Node<T>? previous = null; while (current != null && current.Data != null){ if (current.Data.Equals(data)) { // If the node is in the middle or at the end if (previous != null) { // remove the current node, now previous points to current.Next instead of current previous.Next = current.Next; // If current.Next is not set, then the node is the last one,// change the tail variable if (current.Next == null) tail = previous; } else { // if the first element is being removed // reset the head value head = head?.Next; // if the list is empty after removal, reset the tailif (head == null) tail = null; } count–; return true; } previous = current; current = current.Next; } return false; } |
The algorithm for deleting an element is the following sequence of steps:
- Finding an element in a list by iterating over all elements
- Sets the Next property of the previous node (relative to the one being deleted) to the next node relative to the one being deleted.
The variable is used to track the previous node previous. If an element is found and the variable previous is null, deletion starts over, and in this case, the variable head (the head element) is reset.
If the previous is not null, then the steps of the algorithm described above are implemented.
The complexity of this algorithm is O(n). Graphically, deletion can be represented as follows:

To check if an element exists, the Contains method is used:
1 2 3 4 5 6 7 8 9 10 11 | public bool Contains(T data) |
Here again, a simple enumeration is performed. The algorithmic complexity of the method is O(n).
And for the list to be iterated over in an external program using a for-each loop, the list class implements the IEnumerable interface :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // implementation of the IEnumerable interfaceIEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable<T>)this).GetEnumerator(); } IEnumerator<T> IEnumerable<T>.GetEnumerator(){ Node<T>? current = head; while (current != null) { yield return current.Data; current = current.Next; } } |
Implementing this interface isn’t an integral part of singly linked lists, but it does provide an efficient method for iterating over a collection in a foreach loop. Otherwise, we’d have to implement our own list-iterating constructs.
Using LinkedList:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | LinkedList<string> linkedList = new LinkedList<string>{ // adding elements “Tom”, “Alice”, “Bob”, “Sam” }; // printing elementsforeach (var item in linkedList) { Console.WriteLine(item); } // removing an elementlinkedList.Remove(“Alice”); Console.WriteLine(“\nAfter removing Alice:”); foreach (var item in linkedList) Console.WriteLine(item); // checking if an element existsbool isPresent = linkedList.Contains(“Sam”); Console.WriteLine(isPresent ? “Sam is present” : “Sam is absent”);
|
Console output:
Tom Alice Bob Sam After deleting Alice Tom Bob Sam Sam is present After adding Billa Bill Tom Bob Sam
Explore More IT Terms
#
- Using an integrating factor
- Equations in total differentials
- Bernoulli's equation
- Linear differential equations of the first order
- 10 Mixed C++ Challenges to Test Your Skills
- 11 Tricky Non-Technical Questions to Ask in a Recruiting Interview
- 50 Terms Every Programmer Should Know
- 52 Java Thread Interview Questions
- 7 Levels of Using the Zip Function in Python
- 7 Python Code Bugs You Need to Fix
- 70+ Free Resources for Learning Programming
A
- A Comprehensive Guide to HTML and CSS
- A Comprehensive Guide to MODX CMS
- A computer science question tests your ability to work with IP addresses and network masks
- A Guide to SQL Query Formatting for Beginners
- A/B testing
- Abstract Data Type (ADT)
- AES Encryption Algorithm: How It Works and Where It's Used
- Agile
- Algorithm
- Algorithm Analysis
- Algorithm Complexity-Key Points
- Algorithm complexity: deep parsing O(log n)
- Algorithm vs. Program
- Algorithms and Data Structures in C#
- An overview of the C # programming language
- An overview of the Python programming language
- Anaconda Python
- Analysis of graphs and adjacency matrices question
- Android
- Android App Bundle
- Android SDK
- Angular
- Ansible
- Apache
- Apache Airflow
- Apache Kafka
- Apache Tomcat
- App Store
- AppCode
- Applications of microcontrollers: From simple circuits in electronics to complex systems
- Applications of the derivative
- Arduino: How to Program It: Basics for Beginners
- Array-based stack
- ArrayList
- ASCII
- ASP.NET
- Assembly Language Lessons
B
C
- C++ Lessons
- Cache
- Calculating Memory Capacity
- Character sets and encodings
- Characteristics of a good Algorithm
- Circular singly linked list
- Coding Tests with Answers
- Combinational Circuits Solved Questions
- Compiler
- Complexity of algorithms-Tutorial
- Computer Science Fundamentals Test with Answers (47 questions)
- Computer science tests with answers (More than 100 Questions)
- Constants
- Creating a Table in HTML
- Creating Forms in HTML
- Creating Lists
- Cybernetics and informatics
- Cycles
D
- Data Analytics: applications of data analysis in companies
- Data Engineer - Who is it, what does a data engineer do, and an overview of the profession
- Data modeling: what it is, types, and process steps.
- Data preprocessing: a complete guide for beginners and professionals.
- Data structure
- Data Structures and Algorithms (DSA)
- Data types vs. Data structures
- Database Tests with Answers
- Deep Learning
- Defining Aliases
- Defining Arrays
- Deque
- Detailed articles and tutorials on PHP
- Developing a Website from Scratch
- Differential Equations
- Differentiation of functions
- Digital data: understand the importance of this asset for businesses.
- Double integrals
- Doubly linked lists
- DSA Tutorial
E
F
G
H
- Handling errors and exceptions
- Heads or Tails? How Probability Theory Is Used in IT
- History of the development of computer science
- Homogeneous equations
- Homogeneous vs. non-homogeneous structures
- How to convert numbers between binary, octal, and hexadecimal number systems
- How to effectively organize your workflow
- How to Learn Java: Tips for Beginner Developers
- How to Learn PHP: A Beginner's Guide
- How to Use S3 Storage in Kubernetes with CSI
- HTML
- HTML and CSS: Definition, Application, and Operating Principles
- HTML and CSS. Layout from Scratch: What to Learn, Where to Learn, and How Long Will It Take?
- HTML Frame Structure
- HTML Link Formatting
- HTML Quizzes with Answers
I
- if..else construction
- Infinite sequences and series
- Information properties
- Inheritance in Java: A Complete Guide to Principles and Implementation
- Inserting an Image
- Integration of functions
- Interactive Python Tutorial – Learn Programming from Scratch
- Interpreter
- Interview Problem: Finding a Deleted Element in O(N)
- Interview Scare: The FizzBuzz Challenge
- Introduction to C++
- Introduction to Machine Learning
- Introduction to Machine Learning with Python
- Introduction to Networking | Network Fundamentals Part 1
- Introduction to Number Systems (Binary, Octal, Hexadecimal) | Math for CS Foundations #1
- IT Specialist Resume (CV)
J
K
L
M
- Machine Learning
- Machine Learning Basic Tool: NumPy
- Machine Learning Basic Tool: Pandas
- Machine Learning Basics
- Machine Learning Mathematics
- Machine Learning with Python
- Mathematics for programmers: what is really needed?
- MD5 encryption algorithm: What is it and why is it needed?
- Microcontroller and Microprocessor - what's the difference?
- ML Engineer: Who They Are, What They Do, How Much They Earn, and How to Become a Neural Network Specialist
- Monte Carlo Simulation: How It Works and What It's For
O
P
- PHP lessons
- Private DNS server and its configuration
- Program code
- Programmer's Dictionary
- Programming
- Programming with pseudocode
- Python Code Formatting Guide: PEP8
- Python for data analysis: how to do it and main libraries
- Python Lessons
- Python Superstar: 5 Ways to Use the * Operator
- Python vs. Julia: Should You Replace Python with Julia?
Q
R
S
- Setting up model parameters
- SFML Graphics Library Tutorials
- Sorting Algorithms in Programming: Types, Descriptions, and Comparisons
- SQL commands: see what they are, what the main ones are + examples
- SQL Interview Questions and Tasks
- SQL Lessons
- SQL Stored Procedures
- SQL Syntactic Sugar: The COALESCE Function
- Stack
- Start in analytics: Python or R
- Static vs. dynamic data structures
- Statistical analysis: importance for decision making.
- String formatting in Python
- Structure of computer science
- Supervised learning
- Swift Lessons
- switch/match construct
- Syntax
T
- Terms in programming
- Text and paragraph formatting tags
- The Complete Guide to Bootstrap
- The Complete Guide to JavaScript
- The concept of information and its transmission
- The Future of Python: Key Trends and Insights from Global Researc
- The Infrastructure of Code: A Complete Guide to Repositories for Languages, Frameworks, and Compilers
- The pip package manager in Python
- The role of informatization in the development of society
- Transfers
- Tricky Java Questions Often Asked in Interviews
- Tutorials / Articles
- TypeScript: What It Is and Why Developers Need It
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is .NET and what is it used for?
- What is a data structure?
- What is a GPU in a computer, in simple terms?
- What is a quantum computer: 100,500 problems in one second
- What Is an Algorithm?
- What is Arduino: How it Works and the Platform's Capabilities
- What is Big Data? Introduction, Types, Characteristics, and Examples
- What is FizzBuzz Challenge?
- What is Golang and what is it used for?
- What is Haskell and what is it used for?
- What is Kotlin and what is it used for?
- What is Linux? The History of Linux
- What is machine learning, and how does it work?
- What is Power BI: everything about the data analytics software
- What is recursion, recursive and iterative process in programming?
- What is the C++ programming language?
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- What's the difference between x86 and ARM processors?
- Where to start learning the C programming language?
- Which Linux distribution should you choose? A Linux distribution overview




