algoritmauzmanı
TREN
3.1Singly Linked List
Lesson 3.1.3

Traversal

In an array you jump straight to an element by its . A linked list has no such shortcut: the only way to reach a node is to start at the front and follow the links one by one. This walk is called . Many operations, such as searching, inserting at the end and deleting, contain a traversal inside them.

What you will learn
  • How a helper moves through the list node by node
  • Why the loop stops when it sees and why it never runs on an empty list
  • Why traversal takes time and why we do not walk head itself
5 minTime O(n)Memory O(1)C · Java · Python

1Idea

To traverse, we copy the value of into a helper called temp. On each turn of the loop we print the value of the node temp points to, then move temp to the address in that node's field.

The last node's next field is , so after the last turn temp becomes NULL too and the loop ends. If the list is empty, head is already NULL; the loop does not run even once and only NULL is printed. The empty list needs no separate check.

We walk a helper pointer, not head. If we moved head itself, the address of the start of the list would be lost once the walk ended.

2Step by step

The drawing traverses the list holding 12, 7 and 3. The temp pointer moves one node at each step; the printed output is in the step captions.

NULL12?0x1a407?0x1a903?0x1b20headtemp
  1. temp takes the value of and points to the first node (12). head is not touched.
  2. temp is not NULL, so we enter the loop. 12 is printed. Screen: 12 ->
  3. temp moves to the address in 12's field and now points to 7.
  4. 7 is printed. Screen: 12 -> 7 ->
  5. temp moves one more node and points to the last node (3).
  6. 3 is printed. Screen: 12 -> 7 -> 3 ->
  7. The last node's next field is . temp now points to NULL.
  8. The loop condition is false and the loop ends. NULL is printed at the end. Screen: 12 -> 7 -> 3 -> NULL. head is still on the first node.

struct Node {    int data;    struct Node* next;}; void printList(struct Node* head) {    struct Node* temp = head;    while (temp != NULL) {        printf("%d -> ", temp->data);        temp = temp->next;    }    printf("NULL\n");}
class Node {    int data;    Node next;    Node(int data) { this.data = data; }} void printList(Node head) {    Node temp = head;    while (temp != null) {        System.out.print(temp.data + " -> ");        temp = temp.next;    }    System.out.println("NULL");}
class Node:    def __init__(self, data):        self.data = data        self.next = None def print_list(head):    temp = head    while temp is not None:        print(temp.data, end=" -> ")        temp = temp.next    print("NULL")

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(n)

Each node is visited once. On a list of n nodes the loop runs n times.

Memory
O(1)

However long the list is, only one helper pointer is used.

Searching for a value and inserting at the end make the same walk, which is why they are also in the worst case.

4Common mistake

A common mistake is writing the loop condition as temp->next != NULL:

while (temp->next != NULL) {    printf("%d -> ", temp->data);    temp = temp->next;}

This loop stops on the last node but never prints its value; the output is 12 -> 7 -> . Worse, if the list is empty, temp is and reading temp->next crashes the program. To process every node the condition must be temp != NULL. When you need to stop on the last node (for example when inserting at the end), the next condition is right, but then the empty list is checked separately.

5Try it

When the loop ends, what does temp point to?

Show the answer

The answer is B. The loop only ends once temp is . To stop on the last node you have to change the condition; you will see this in inserting at the end.

How many times does the traversal loop run on a list of 5 nodes?

Show the answer

The answer is B. The loop runs once for each node. On the sixth check temp is and the loop is not entered.

Lessons you mark show up in the curriculum. This is kept on this device only.