Singly Linked List

So you want to store a list of values. But you don’t know how many you’ll have. An array makes you pick a size up front. Pick too small and you run out of room. Pick too big and you waste memory. A singly linked list fixes this. It grows one piece at a time. Each piece only knows where the next piece is.

📚 What Is a Singly Linked List?

A singly linked list is a chain of small boxes. Each box is called a node. A node holds two things.

  • The actual value you want to store. This could be a number or a name.
  • A link that points to the next node in the chain.

The first node is special. We keep a pointer to it called the head. The head is just the entry point to the whole list. From the head you can reach every other node. You follow the links one by one.

The last node has nowhere to go next. So its link points to nothing. In C, C++, and Java we call that nothing NULL. In Python it is None. In JavaScript it is null. The idea is the same everywhere. It just means “the chain ends here”.

Think of a treasure hunt. Riya gets the first clue. That clue tells her where the next clue is. She follows it and reads the next clue. This keeps going. The last clue says “you’re done”. The node value is the clue’s message. The link is the address of the next clue.

Head

10 | next

20 | next

30 | null

🎯 Why Not Just Use an Array?

Here is the pain. In an array the items sit next to each other. They live in one fixed block of memory. To add or remove from the middle, you have to shift everything around. And the size is decided early.

A linked list spreads the nodes out in memory. Each node can live anywhere. The link is what holds them together. So the trade-off looks like this.

Question Array Singly Linked List
Size fixed early? Yes No, grows anytime
Jump to index 5 directly? Yes, instant No, walk from head
Nodes stored together? Yes, one block No, spread out

Note

A singly linked list only points forward. You can move from a node to the next one. But you can’t go back to the one before. To go backward you’d need a doubly linked list. That kind keeps a link to the previous node too.

🛠️ Steps to Build and Traverse the List

We’ll do two jobs here. First we build a small list with the values 10, 20, 30. Then we walk through it and print every value. Here is the plan.

  1. Define a node that holds a value and a link to the next node.
  2. Create the first node. Set its value to 10 and point the head at it.
  3. Create a second node with 20. Link the first node’s next to it.
  4. Create a third node with 30. Link the second node’s next to it.
  5. Set the last node’s next to null so the chain ends cleanly.
  6. To traverse, start at the head. Print the current value, then move to the next node. Repeat until you hit null.

When we print, we’ll show the arrows so the shape is clear. It will look like 10 -> 20 -> 30 -> NULL.

💻 Code in Five Languages

The code below defines the node. It links three nodes by hand. Then it traverses from the head and prints each value followed by an arrow.

singly_linked_list.py
# A node holds a value and a link to the next node
class Node:
def __init__(self, value):
self.data = value
self.next = None
# Start at head, print each value, move to next until None
def traverse(head):
current = head
while current is not None:
print(current.data, "-> ", end="")
current = current.next
print("NULL")
# Build the list: 10 -> 20 -> 30
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
traverse(head)

The output of the above code will be:

10 -> 20 -> 30 -> NULL

Tip

Notice the pattern. We never used an index. We only ever said “go to next”. That is the heart of a linked list. You move through it by following links, not by counting positions.

⏱️ How Fast Is It?

Traversing means visiting every node once from head to end. If there are n nodes, you do n steps. So traversal is O(n).

Now what about reading the value at a position? Say you want the 5th node. In an array that is instant. In a linked list you can’t jump. You have to start at the head and follow the links five times. So access by index is also O(n).

Note

The key thing to remember: a linked list has no shortcut to the middle. Even to reach one node by its index, you walk there from the head.

Operation Time Why
Traverse all nodes O(n) Visit each node once
Access value by index O(n) Walk from head, no jumping
Read the head value O(1) Head is right there

⚠️ Common Mistakes

A few slips trip up almost everyone the first time.

  • Forgetting to set the last node’s next to null. Then traversal never stops. Your program reads garbage memory or loops forever.
  • Losing the head pointer. If you move the head forward while building, you lose the start of the list and can’t get back to it. Use a separate current pointer to walk.
  • Reading next on a null node. Once current becomes null, the list is over. Trying to read current.next after that crashes.
  • Assuming you can jump to the middle like an array. You can’t. Always start from the head and follow links.

🧩 What You’ve Learned

✅ A singly linked list is a chain of nodes. Each node holds a value and a link to the next node.

✅ The head pointer is the entry point. The last node’s next points to null to mark the end.

✅ You build a list by creating nodes and setting each node’s next to the following node.

✅ You traverse by starting at the head and following next until you reach null.

✅ Traversal is O(n). Access by index is O(n) too, because there’s no way to jump straight to a position.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    In a singly linked list, what does the last node's next point to?

    Why: The last node has no node after it, so its next points to null to mark the end of the list.

  2. 2

    What does the head pointer give you?

    Why: The head is the starting point. From it you follow links to reach every other node.

  3. 3

    What is the time complexity of accessing the value at a given index in a singly linked list?

    Why: You can't jump to a position, so you walk from the head following links, which takes O(n).

  4. 4

    How do you move through a singly linked list?

    Why: A singly linked list only points forward, so you move by following the next link from node to node.

🚀 What’s Next?

You can build a list and walk through it now. Next, let’s learn how to add and find nodes.

Share & Connect