DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

πŸš€ Add Element at the End of a Linked List (0 Monster Level)

🧱 1. What is a Linked List?

A Linked List is a collection of nodes connected like a chain.

Each node contains:

  • value β†’ data
  • next β†’ reference (link) to the next node
[value | next] β†’ [value | next] β†’ [value | null]
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ The last node always points to null.


🧠 2. Core Idea (Very Important)

To add a node at the end:

β€œStart from head β†’ move step by step β†’ find last node β†’ connect new node”


🟒 3. Empty List Case (MOST IMPORTANT START)

If the list is empty:

head = null
Enter fullscreen mode Exit fullscreen mode

Add 5:

We create:

newNode = { value: 5, next: null }
Enter fullscreen mode Exit fullscreen mode

Now:

head β†’ 5 β†’ null
Enter fullscreen mode Exit fullscreen mode

βœ” First node becomes the head


🟑 4. First Insertion Example

Insert: 5

```text id="a5"
head β†’ null




After push(5):



```text
head β†’ 5 β†’ null
Enter fullscreen mode Exit fullscreen mode

βœ” Head is updated


Insert: 10

Start:

5 β†’ null
Enter fullscreen mode Exit fullscreen mode

Step 1: Create node


10 β†’ null
Enter fullscreen mode Exit fullscreen mode

Step 2: Traverse from head

current = 5
Enter fullscreen mode Exit fullscreen mode

Check:

5.next === null ❌ stop here
Enter fullscreen mode Exit fullscreen mode

Step 3: Attach node

5.next = 10
Enter fullscreen mode Exit fullscreen mode

Result:

5 β†’ 10 β†’ null
Enter fullscreen mode Exit fullscreen mode

πŸ”΅ 5. Second Insertion (DETAILED DRY RUN)

Insert: 20

Start:

5 β†’ 10 β†’ null
Enter fullscreen mode Exit fullscreen mode

Step 1: Create node

20 β†’ null
Enter fullscreen mode Exit fullscreen mode

Step 2: Start from head

current = 5
Enter fullscreen mode Exit fullscreen mode

Step 3: Move step-by-step

First move:

5 β†’ 10 β†’ null
   ↑ current moves
Enter fullscreen mode Exit fullscreen mode

Now:

current = 10
Enter fullscreen mode Exit fullscreen mode

Step 4: Check next

10.next === null βœ” STOP
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ We found last node


Step 5: Attach new node

10.next = 20
Enter fullscreen mode Exit fullscreen mode

Final result:

5 β†’ 10 β†’ 20 β†’ null
Enter fullscreen mode Exit fullscreen mode

πŸ’» 6. Final Clean Code

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

  push(value) {
    const newNode = new Node(value);

    // Case 1: empty list
    if (!this.head) {
      this.head = newNode;
      return;
    }

    // Case 2: traverse to last node
    let current = this.head;

    while (current.next) {
      current = current.next;
    }

    // attach new node
    current.next = newNode;
  }
}
Enter fullscreen mode Exit fullscreen mode

⚠️ 7. VERY IMPORTANT RULES

βœ” Always check this.head (NOT newNode)
βœ” Never do current = newNode ❌
βœ” Always use current.next = newNode βœ…
βœ” Use while (current.next) for traversal


⏱️ 8. Complexity

  • Without tail: O(n) (you travel whole list)
  • With tail pointer: O(1) (instant insert)

πŸ”₯ 9. Interview-Level Insight

Linked list insertion at end is not about β€œadding value”
It is about β€œchanging references”

You are NOT storing memory addresses manuallyβ€”
JavaScript automatically stores object references.


🧠 Final Mental Model

β€œWalk until null β†’ replace null with new node”

OR

β€œFind last node β†’ connect next pointer”

Top comments (0)