<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abdul Karim</title>
    <description>The latest articles on Forem by Abdul Karim (@karimdevelops).</description>
    <link>https://forem.com/karimdevelops</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3546262%2F9fd011bc-7fa7-4f8e-b399-9f49c513c669.jpeg</url>
      <title>Forem: Abdul Karim</title>
      <link>https://forem.com/karimdevelops</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/karimdevelops"/>
    <language>en</language>
    <item>
      <title>Are Linked Lists necessary?</title>
      <dc:creator>Abdul Karim</dc:creator>
      <pubDate>Mon, 06 Oct 2025 16:14:33 +0000</pubDate>
      <link>https://forem.com/karimdevelops/are-linked-lists-necessary-2ckl</link>
      <guid>https://forem.com/karimdevelops/are-linked-lists-necessary-2ckl</guid>
      <description>&lt;p&gt;For most beginners, linked lists and arrays are pretty much the same linear data structures. The operations are quite the same i.e. both insert, delete, and access elements. However, there are key differences that you need to consider when selecting one of the data structures and to understand the necessity of linked lists.&lt;/p&gt;

&lt;p&gt;I will discuss these differences on the basis of &lt;strong&gt;worst-case scenario time complexity&lt;/strong&gt; mainly. But, before we start, let's recall contiguity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Contiguity
&lt;/h3&gt;

&lt;p&gt;Arrays have elements that are contiguous i.e. elements are next to each other in the memory. Whether they are static or dynamic, this property always holds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6pfirl1gzo0g4huo5cu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6pfirl1gzo0g4huo5cu.png" alt="Array memory block" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, elements in linked lists are non-contiguous, instead they are generally scattered in different places of the memory. It is a node based data structure where all these nodes are linked together and each node has a reference to its previous or next node depending on the type i.e. singly linked list or doubly linked list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ccv3kliqvqwn5g0lex9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ccv3kliqvqwn5g0lex9.png" alt="Linked list diagram" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's take a look at some operations and a data abstract type (DAT) to discuss the necessity of linked lists.&lt;/p&gt;

&lt;h3&gt;
  
  
  Insertion
&lt;/h3&gt;

&lt;p&gt;Insertion means to add an element to our array or linked list. This can be done at the start (prepend), at the end (append), or at any position you like (as long as it is less than the length).&lt;/p&gt;

&lt;p&gt;Let's see what happens when we prepend a letter "G" to an array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftnl8yl7a3wh9in6ctq85.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftnl8yl7a3wh9in6ctq85.png" alt="Array diagram" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl35e3xoozcujegdfk5pd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl35e3xoozcujegdfk5pd.png" alt="Prepending to an array" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how every element moved a step forward because we decided to prepend. The indices (singular: index) also shift respectively. Imagine if we had "n" elements (could be ten, hundred or even a million), how much time would it take? &lt;br&gt;
You guessed it! the time complexity will be Big O of N or &lt;em&gt;O(n)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What would be the time complexity if we append? It's the end, so obviously we will not shift any elements. Therefore, it's time complexity will be O(1).&lt;/p&gt;

&lt;p&gt;Let's discuss about linked lists now! Insertion is all about changing the references (or links :D) between the nodes. For the sake of simplicity, I will discuss it using singly linked list. We are going to prepend the letter "G" again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9f46qo27a3dz3qc8dzqj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9f46qo27a3dz3qc8dzqj.png" alt="Prepending to a linked list" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We simply pointed our node to "A".&lt;/p&gt;

&lt;p&gt;Let me add it next to the letter "B" now.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8c5fpangl4hvtbx4keyq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8c5fpangl4hvtbx4keyq.png" alt="Inserting in a linked list" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, we simply removed the "next" reference of "B" from "C" and moved it towards "G". And also pointed G towards the next node "C".&lt;/p&gt;

&lt;p&gt;Therefore, the time complexity is always O(1) regardless of the insert position.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note: We are not considering the time complexity of traversal here but only of the insertion operation.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deletion
&lt;/h3&gt;

&lt;p&gt;It's simply the opposite of insertion. When we delete from the start of an array or any other position before the last element, the indices will shift back. Time complexity will be O(n).&lt;/p&gt;

&lt;p&gt;Let's delete the letter "A" from our previous array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v22u48er3j9ybrnb30h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v22u48er3j9ybrnb30h.png" alt="Deletion in array" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, all the elements step backwards.&lt;/p&gt;

&lt;p&gt;Similarly in a linked list just like insertion, we simply manipulate the references but this time, we remove them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpn2nl5wpj66i6ga9dvok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpn2nl5wpj66i6ga9dvok.png" alt="Deletion in linked list" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiinictpoznbpo3hkcqi2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiinictpoznbpo3hkcqi2.png" alt="Deletion in linked list" width="800" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time complexity is O(1) similar to insertion.&lt;/p&gt;

&lt;h3&gt;
  
  
  Random Access
&lt;/h3&gt;

&lt;p&gt;Lastly, let's talk about accessing (or getting) elements both in arrays and linked list.&lt;/p&gt;

&lt;p&gt;Random access is the ability to get an element from an array in constant time complexity &lt;em&gt;or O(1)&lt;/em&gt;. Yup, you read that right it's O(1). But why is this only for arrays and not linked lists?&lt;/p&gt;

&lt;p&gt;In linked lists we need to traverse all the nodes until we find our target node, the reason being is that the elements are non-contiguous whilst in arrays, elements are contiguous. We just need to know the offset and we can get any element instantly. Therefore, time complexity of linked lists is O(n).&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Abstract Types (DAT)
&lt;/h3&gt;

&lt;p&gt;To keep this article short, I will quickly discuss a data abstract type i.e. queue.&lt;/p&gt;

&lt;p&gt;Queue follows First In, First Out (FIFO) principle which means we always insert elements at the end, but remove from the beginning. Now, what do you think? If we use an array, and remove an element from the beginning, all the indices will shift which is really bad for the performance. Hence, we go with linked lists.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So, are linked lists necessary? Well, &lt;em&gt;it depends&lt;/em&gt;. If your priority is fetching elements frequently then you should go with arrays, however, if you are going to do multiple insertions and deletions like in queue, you should go with linked lists preferably.&lt;/p&gt;

&lt;p&gt;Therefore, it's not about the preference, but rather your use case!&lt;br&gt;
Your choice will greatly impact the performance habibi.&lt;/p&gt;

&lt;p&gt;Below is a table showing worst-case scenarios for the respective data structures.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf8plb9p0pc8iny75b33.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf8plb9p0pc8iny75b33.png" alt="Comparing Table" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>datastructures</category>
      <category>programming</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
