algoritmauzmanı
TREN
3.1Singly Linked List
Lesson 3.1.1

What is a singly linked list?

The singly linked list is the simplest kind of linked list. Each element lives in its own and knows only the one after it. In this lesson you will see what a node is made of, where the list starts and ends, and how the node is defined in three languages.

What you will learn
  • The two fields of a node, data and
  • The job of the pointer and why the list ends with
  • What the empty list looks like and how the node is defined in C, Java and Python
5 min

Each element of the list is a . A node has two fields: data, which holds the value, and , which holds the address of the node after it. next is a ; it does not hold a value but where the following node sits in memory.

datanextNULL12?0x1a40
One node: the value (12) on the left, the next field on the right. 0x1a40 below it is the node's memory address. No node follows this one, so next is NULL.

Nodes do not have to sit side by side in memory. Each one is allocated in its own place; the order comes from the next fields, not from where the nodes are in memory.

2head and the end of the list

A pointer called points to the first node of the list. You always enter the list through head: you reach the first node with head, the second with the first node's next field, the third with the second node's next field.

NULL12?0x1a407?0x1a903?0x1b20head
The list 12 → 7 → 3. head points to the first node; the last node's next field is NULL.

The last node's next field holds . NULL means "there is no node here". Code that walks () the list knows from NULL that the list has ended.

The links only point forward, so you cannot go back in the list. There is no way from 7 back to 12; to go back you start again from head. That is why the list is called singly linked.

3The empty list

In a list with no nodes, points to . Every operation on a list has to think about this case: an empty list has no first node and head->next cannot be read.

head = NULL
The empty list: no nodes, head is NULL.

The length of the list is not stored anywhere. To find out how many nodes there are, you count them from start to end.

4The node in code

In C the node is defined as a . The next field is a pointer to a structure of the same type. Nodes are allocated at run time with .

struct Node {    int data;            // the value    struct Node* next;   // address of the next node}; struct Node* head = NULL;   // empty list

In Java the node is a class and the next field holds a to another Node object. In a new object this field is null on its own.

class Node {    int data;    // the value    Node next;   // the next node (null at first)    Node(int data) { this.data = data; }} Node head = null;   // empty list

In Python the node is a class too. An empty link is written as None.

class Node:    def __init__(self, data):        self.data = data   # the value        self.next = None   # the next node head = None  # empty list

These definitions stay the same in every lesson of this part. The next step is to build a list from these nodes.

5Try it

In a singly linked list, what is in the last node's next field?

Show the answer

The answer is B. No node comes after the last one, so its field is . A list whose last node points back to the first is a circular linked list.

What does it mean when head is NULL?

Show the answer

The answer is B. points to the first node. If there is no node for it to point to, the list has no nodes.

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