3.1Singly Linked List
- 3.1.1What is a singly linked list?
- 3.1.2Creating a singly linked list
- 3.1.3Traversal
- 3.1.4Inserting at the end
- 3.1.5Inserting at a position
- 3.1.6Inserting at the head
- 3.1.7Deleting from the end
- 3.1.8Deleting from the head
- 3.1.9Deleting from the middle
- 3.1.10Searching for a value
- 3.1.11Reversing a linked list (iterative)
- 3.1.12Reversing a linked list (recursive)
What is a singly linked list?
The singly linked list is the simplest kind of linked list. Each element lives in its own node 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.
1The node: a value and a link
Each element of the list is a node. A node has two fields: data, which holds the value, and next, which holds the address of the node after it. next is a pointer; it does not hold a value but where the following node sits in memory.
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 head 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.
The last node's next field holds NULL. NULL means "there is no node here". Code that walks (traverses) 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, head points to NULL. Every operation on a list has to think about this case: an empty list has no first node and head->next cannot be read.
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 struct. The next field is a pointer to a structure of the same type. Nodes are allocated at run time with malloc.
struct Node { int data; // the value struct Node* next; // address of the next node}; struct Node* head = NULL; // empty listIn Java the node is a class and the next field holds a reference 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 listIn 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 listThese 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?
The answer is B. No node comes after the last one, so its next field is NULL. A list whose last node points back to the first is a circular linked list.
Show the answer
The answer is B. No node comes after the last one, so its next field is NULL. A list whose last node points back to the first is a circular linked list.
What does it mean when head is NULL?
The answer is B. head points to the first node. If there is no node for it to point to, the list has no nodes.
Show the answer
The answer is B. head 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.