Watch a singly linked list prepend, append, search and delete the head, one pointer at a time. Worked example on 10 to 70, then type your own values to try it.
About this algorithm
A singly linked list stores items in separate nodes, each holding a value and one next field that names the node after it. The list itself only remembers the head, the first node, and here also the tail, the last one. Nothing sits at a fixed address, so adding or removing an item means rewriting a pointer or two, and finding an item means following the chain from the front.
Choose it over an array when items are mostly added and removed at the front, or at the back with a tail pointer, and you rarely need the k-th item: a stack, a simple queue, or a free list of spare objects. An array wins when you read by position, because arr[i] is one lookup while a list must step from the head i times, and its items sit together in memory, which is faster to scan. When you need to step backwards, or delete a node you already hold, see the Doubly Linked List page; for a list whose end loops back to its start, see Circular. The classic interview tasks on this structure each have their own page: Reverse, Find Middle and Remove N-th From End.
Each node is a small object, { value, next }. The last node has next = null, which is how every walk knows to stop. An empty list is just head = null.
Set current to head and an index to 0. While current is not null, compare current.value with the target; on a match return the index, otherwise move current to current.next and add one. Falling off the end returns -1.
Make the new node, point its next at the old head, then move head to the new node. The order matters: moving head first would lose the rest of the chain.
This page keeps a tail pointer, so append sets tail.next to the new node and moves tail onto it, with no walk. Without a tail field you would have to walk from the head to find the last node first.
Removing the head is head = head.next; if that leaves the list empty, tail becomes null too. To cut out a node further in, you need the node before it and set before.next = before.next.next, which usually means a walk from the head to find that earlier node.
The page starts with seven nodes, 10, 20, 30, 40, 50, 60, 70, head on 10 and tail on 70. Press Play with the box empty and it searches for 40: current visits 10, 20 and 30 without a match and stops on 40, returning index 3. Type 5 and press Prepend: the new node points at 10 and head moves to 5, so the list has 8 nodes. Type 80 and press Append: 70.next becomes 80 and tail moves to 80, again with no walk. Searching for 80 now takes 9 comparisons and returns index 8, the far end. Press Del Head: head moves from 5 back to 10 and 5 drops out of the chain. Search for 99 and current checks all 8 nodes, reaches null and returns -1.
The same list class also drives the doubly linked list page, so the code shown for Prepend, Append and Del Head includes lines that set a prev field. The drawing here shows next arrows only; a true singly linked list simply leaves those lines out, and every step above still holds.
Time: prepend, append with the tail pointer, and deleting the head each rewrite a fixed number of fields, O(1), while search may have to visit every one of the N nodes, O(N). Space: one node with a value and a next field per item, O(N) overall, and each operation uses only a pointer or two extra.