Singly Link List
Singly Link List
CHAPTER
LINKED LISTS
3
3.1 What is a Linked List?
static structures and therefore cannot be easily
One disadvantage of using arrays to store data is that arrays are maintain new insertions and deletions. In this
extended or reduced to fit the data set. Arrays are also expensive to
chapter we consider another data structure called Linked Lists that addresses some of the limitations of arrays.
data. A linked list has the following properties. A
A linked list is a data structre used for storing collections of
number of nodes in a list is not fixed and can grow and shrink
linked list is a linear dynamic data structure. The
- the data and a reference to the next node. The
on demand, Each node of a linked list is made up of two items
list is called the head of the list. It should be noted
last node has a reference to null. The entry point into a linked empty then the head is a null
the list is
that head is not a separate node, but the reference to the first node. If
reference.
Successive elements are connected by pointers.
The last element points to NULL.
Can grow or shrink in size during execution of a program.
Can be made just as long as required (until systems memory exhausts).
pointers). It allocates memory as list
Does not waste memory space (but takes some extra memory for
grows.
NULL
head
Advantages of Arrays
Simple and easy to use
access)
Faster access to the elements (constant
are frequently being added and deleted, and the location of new elements added to the list is significant, then
benefits of a linked list increase.
Parameter Linked list Array Dynamic array
Indexing O(n) O(1) O(1)
Insertion/deletion at Ofn), if array is not full (for shifting the O(n)
beginning O(1) elements)
O(1), if array is not
O(1), if array is not full full
Insertion at ending O(n) O(n), if array is full
Deletion at ending O(n) O(1) O()
Ofn), if array is not full (for shifting the O(n)
Insertion in middle O(n) elements)
O(n), if array is not full (for shifting the O(n)
Deletion in middle O(n) elements)
O(n) (for pointers) O
(n)
Wasted space
3.6 Singly Linked Lists We can think of each node as a record. The first part
The linked list consists of a series of structures called nodes. part of the record a feld that stores apointer to a
the second
of the record is a field that stores the data, and next field which is a pointer used to link one node to
node. So, each node contains two fields: a data field and a which
linked list. This list consists of a number of nodes in
the next node. Generally "linked list" means a singly the last node in the list is NULL, which indicates
The link of
each node has a next pointer to the following element.
the end of the list.
the node memory continues to exist until it is explicitly
Each node is allocated in the heap with a call to malloc(), so node in the list. The last node's next pointer
deallocated witha call to free). The node called a head is the first
points to NULL value.
40 NULL
Head
head
The function given
function takes a linked list as input and counts the number of nodes in the list.
The length()
data with extra print function.
below can be used for printing the list
void print(struct ListNode *head) {
struct ListNode *cur - head; cur->next) {
for (cur = head; cur != NULL; cur =
printf"%d ", c°->data);
45
3.6 Singly Linked Lists
Data Structures and Algorithmns Made Easy
Linked List
printf("\n");
|/Calculating the size of the list
int length(struct ListNode *head) {
struct ListNode *cur = head;
int count =0;
for (cur =head: cur = NULL: cur = Cur->next) {
countt+;
cur = cur->next;
return count;
Thead
Update head pointer to point to the new node.
new node
data 15 40 NULL
Thead
struet ListNode *insertAtBeginning(struct ListNode *head, int data){ //add to beginning of list
struct ListNode *temp;
1/ create a new temp node
temp-(struct ListNode *}malloc(sizeof(struct ListNode)); //create unnamed node and pointer
temp->data-data; //set temp data value
temp->next=NULL; //set as end of list
if (head= NULL){// then list empty, so set as head node
head-temp;
head->next=NULL;
else{// else add to left of list
temp->next-head;
head-temp;
return head:
4 15 data - ºNULL
head
Last nodes next pointer points to the new node. new node
- NULL
15
- data
head
new node
head
data
head
data
new node
Let us write the code for all three cases. We must update the first element pointer in the calling furnction, not just
n the called function. For this reason we need to send a double pointer. The following code inserts a node in the
singly linked list.
3.6 Singly Linked Lists 47
Data Structures and Algorithms Made Easy Linked Lists
1/Inserts a new node (ne) at position n and
struct ListNode *insertAtGivenPosition( structreturns
ListNodehead.
*head, struct ListNode *new, int n)?
1/pred will point to the predecessor of new
struct ListNode *pred = head;
1/Special case: adding at position 1 (nserting at head)
if ( n<= 1){
new -> next = head:
returm nevw;
17find the n-1 node (predecessor): decrement n and move down the list
1/until either the list has ended or n becomes 0
while ( --n && pred NULL )
pred = pred -> next; //repoint temp to the next element
if (pred == NULL )
return NULL;
7/ifit is not NULL, insert new after its predecessor
new -> next = pred -> next;
pred -> next = new;
return head;
Time Complexity: O(n). In the worst case, we may need to insert the iten at the end of the list. Space Complexity:
O(1).
Note: We can implement the three variations of the insert operation separately.
Time Complexity: O(n), since, in the worst case, we may need to insert the node at the end of the list. Space
Complexity: O(1).
Similar to insertion, here we also have three cases.
Deleting the frst node
Deleting the last node
Deleting an intermediate node.
Head Temp
Now, move the head nodes pointer to the next node and dispose of the temporary node.
15 40 NULL
Temp Head
Time Complexity: O(n), for scanning the list of size n. Space Complexity: O(1).
Deleting the Last Node in Singly Linked List
In this case, the last node is removed rom the list. This operation is a bit trickier than removing the frst node,
becese the algoríthm should find a node, which is previous to the tail, It can be done in three stens:
Traverse the list and while traversing maintain the previous node address also. By the time we reach the
end of the list, we will have two pointers, one pointing to the tail node and the other pointing to the node
before the tail node.
4 15 40 +NULL
Head
Node previous to tail Tail
Update previous node's next pointer with NULL.
NULL
4 15 7 40 NULL
Head
Previous node to tail Tail
Dispose of the tail node.
NULL
4 NULL
temp->next = NULL;
free(current);
return;
40 NULL
4 15
Node to be deleted
Head Previous node
deleted.
Dispose of the current node to be
40 NULL
15
p =p->next;
Time Complexity: O(n). In the worst case, we may need to delete the node at the
Space Complexity: O(1). end of the list.