DEV Community

Cindy Lam
Cindy Lam

Posted on

4 1

Linked List - Basics

Linked List Brief Intro:

  • Linear collection data structure
  • Chain of nodes ⛓ - "each node contains a value and a pointer to the next node in the chain."
Singly Linked List (one direction only)

1 -> 2 -> 3 -> 4 -> null (when the pointer gets to null, it has reached the end of the linked list) 
^
Head: beginning of the linked list 
Enter fullscreen mode Exit fullscreen mode
//Linked List Node Class Declaration
class ListNode { 
   constructor(value = 0, next = null) { 
      this.value = value; 
      this.next = next; 
   }
}
Enter fullscreen mode Exit fullscreen mode

In this Linked List series, I will be using curr (stands for current) as the main pointer to move through a Linked List.

//Using curr allows us to remember the head to return it at the end of the function

let curr = head; 
Enter fullscreen mode Exit fullscreen mode
1 -> 2 -> 3 -> 4 -> null
^
curr

curr = ListNode { 
          value: 1, 
           next: ListNode { 
                   value: 2, 
                    next: ListNode { 
                             value: 3, 
                              next: ListNode {
                                     value: 4, 
                                      next: null
                                   }
                          }
                 }
       }
curr.value = 1
Enter fullscreen mode Exit fullscreen mode
1 -> 2 -> 3 -> 4 -> null
^ ->
curr.next (the next node based on the current node) 

curr.next = ListNode{2, ListNode{3, ListNode{4}}}
curr.next.value = 2
Enter fullscreen mode Exit fullscreen mode

How to move to the next node in a Linked List?

//assigning curr to the next node 
curr = curr.next; 
Enter fullscreen mode Exit fullscreen mode

Here is an example:

while(curr) { //keep iterating as long as curr is not null
   curr = curr.next; 
}
Enter fullscreen mode Exit fullscreen mode
While 'curr' is not null: 

1 -> 2 -> 3 -> 4 -> null
^ ->
curr.value = 1
curr.next.value = 2

curr = curr.next;

__

While 'curr' is not null: 

1 -> 2 -> 3 -> 4 -> null
     ^ ->
curr.value = 2
curr.next.value = 3

curr = curr.next;

__

While 'curr' is not null: 

1 -> 2 -> 3 -> 4 -> null
          ^ ->
curr.value = 3
curr.next.value = 4

curr = curr.next;

__

While 'curr' is not null: 

1 -> 2 -> 3 -> 4 -> null
               ^ ->
curr.value = 4
curr.next = null

curr = curr.next; 

__

1 -> 2 -> 3 -> 4 -> null
                     ^ ->
'curr' is null, stop the iteration. 
The pointer has now moved through the entire Linked List. 
Enter fullscreen mode Exit fullscreen mode

References & Additional Resources:

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay