0% found this document useful (0 votes)
3 views

Linked list algorithms

The document outlines various algorithms for creating, traversing, counting, searching, inserting, and deleting nodes in a linked list. It provides step-by-step procedures for each operation along with their respective time complexities, primarily O(n) for most operations and O(1) for inserting at the beginning or deleting the first node. Additionally, it includes examples to illustrate the linked list structure and operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linked list algorithms

The document outlines various algorithms for creating, traversing, counting, searching, inserting, and deleting nodes in a linked list. It provides step-by-step procedures for each operation along with their respective time complexities, primarily O(n) for most operations and O(1) for inserting at the beginning or deleting the first node. Additionally, it includes examples to illustrate the linked list structure and operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Linked list algorithms

Creating a linked list (nodes added at end of list)


1: begin /* data is taken in variable num */
2: head ← NULL
3: ptr ← NULL
4: enter the value for num
5: while num != -1 steps 6, 7, 8 and 9 // != means not equal to
6: create newNode
7: newNode -> data ← num
8: newNode->next ← NULL
9: if head equals to NULL
head ← newNode
ptr ← newNode
else
ptr->next ← newNode
ptr ← ptr->next
endif
enter the value for num
10: endwhile /* num = -1 breaks the loop */
11: end
Time complexity: O(n)

Let us create a linked list, using the above algorithm, having data 10, 15, 20 and 12:

After the first node is created, the linked list looks like:
10

head prt

After the second node is created, the linked list looks like:
10 15

head prt

After the third node is created, the linked list looks like:
10 15 20

head ptr

After the fourth node is created, the linked list looks like:
10 15 20 12

head ptr

Prepared by Somenath Sengupta Page 1 of 7


Linked list algorithms

Traversing a linked list


1: begin
2: ptr ← head
3: repeat steps 4 and 5
4: access ptr -> data /*eg. print or store */
5: ptr ← ptr->next
6: until ptr != NULL
7: end
Time complexity: O(n)

Counting nodes in a linked list


1: begin
2: count ← 0
3: ptr ← head
4: repeat steps 5 and 6
5: count ← count + 1
6: ptr ← ptr->next
7: until ptr != NULL
8: end
Time complexity: O(n)

Searching through a linked list


/** value stores the data that needs to be searched **/
1: begin
2: ptr ← head,
3: position ← NULL
4: repeat step 5
5: if value equals to ptr -> data then do
position ← ptr /* save position */
go to step 8
else
ptr ← ptr->next
endif
6: until ptr != NULL
7: if position equals to NULL then
value does not exist
8: endif
9: end
Time complexity: O(n)

Inserting a Node into a Linked List – Three cases


a. Inserting at the end of a linked list
b. Inserting at the beginning of a linked list
c. Inserting between two nodes

Prepared by Somenath Sengupta Page 2 of 7


Linked list algorithms

a. Inserting at the end of a linked list

1: begin
2: create newNode
3: newNode->data ← num
4: newNode->next ← NULL
5: ptr ← head
/* make ptr to point to last node */
6: repeat step 7
7: ptr ← ptr->next
8: until ptr->next != NULL
9: ptr->next ← newNode
10: end

Time complexity: O(n)

The linked list looks like:


10 15 20 12 7

head newNode

10 15 20 12 7

head ptr newNode

10 15 20 12 7

head ptr newNode

b. Inserting at the beginning of a linked list

1: begin
2: create newNode
3: newNode->data ← num
/* points to the previous first node */
4: newNode->next ← head
5: head ← newNode /*updating the head */
6: end

Time complexity: O(1)

Prepared by Somenath Sengupta Page 3 of 7


Linked list algorithms

c. Inserting between two nodes

/*insert after node with value */


1: begin
2: create newNode
3: newNode->data ← num
4: ptr ← head
5: repeat steps 6, 7
6: preptr ← ptr
7: ptr ← ptr->next
8: until preptr->data != value
9: preptr->next ← newNode
10: newNode->next ← ptr
11: end

Time complexity: O(n)

/*insert before node with value */


1: begin
2: create newNode
3: newNode->data← num
4: ptr ← head
5: repeat steps 6, 7
6: preptr ← ptr
7: ptr ← ptr->next
8: until ptr->data != value /* only change */
9: preptr->next ← newNode
10: newNode->next ← ptr
11: end

Time complexity: O(n)

Deleting Nodes of a Linked List – Three cases

a) Deleting the first node of a linked list


b) Deleting the last node of a linked list
c) Deleting nodes from a given position
i) deleting from k-th position
ii) deleting a node with a certain value
iii) deleting a node after a node with a certain value

Prepared by Somenath Sengupta Page 4 of 7


Linked list algorithms

a) Deleting the first node of a linked list

1: begin
2: check underflow condition
3: ptr ← head
/* updates head to the new first node */
4: head ← ptr->next
5: free ptr /*deallocating the head */
6: end

Time complexity: O(1)

The linked list looks like:

10 15 20 12 7

head

10 15 20 12 7

head ptr

10 15 20 12 7

ptr head

15 20 12 7

head

b) Deleting the last node of a linked list

1: begin
2: check underflow condition
3: ptr ← head
4: repeat steps 5, 6
5: preptr ← ptr
6: ptr ← ptr->next
7: until ptr->next != NULL
8: preptr->next ← NULL
9: free ptr /*deallocating the ptr */
10: end

Prepared by Somenath Sengupta Page 5 of 7


Linked list algorithms

c) Deleting nodes from a given position


i) deleting from k-th position

1: begin
2: check underflow condition
3: ptr ← head
4: repeat steps 5, 6
5: preptr ← ptr
6: ptr ← ptr->next
7: until preptr->data != value
8: preptr->next ← ptr->next
9: free ptr /* deallocating the ptr */
10: end

Consider the following linked list:


10 15 20 12 7

head

To delete node with value 12, the (logical) steps are as follows:
10 15 20 12 7

head

10 15 20 12 7

head

10 15 20 12 7

head

ii) deleting a node with a certain value

1: begin
2: check underflow condition
3: ptr ← head
4: preptr ← head
5: repeat steps 6, 7
6: preptr ← ptr
7: ptr ← ptr->next
8: until ptr->data != value
9: preptr->next ← ptr->next
10: free ptr /* deallocating the ptr */
11: end

Prepared by Somenath Sengupta Page 6 of 7


Linked list algorithms

iii) deleting a node after a node with a certain value

/*delete after a node with certain value */


1: begin
2: check underflow condition
3: ptr ← head
4: preptr ← head
5: repeat steps 6, 7
6: preptr ← ptr
7: ptr ← ptr->next
8: until preptr->data != value
9: preptr->next ← ptr->next
10: free ptr /* deallocating the ptr */
11: end

Time complexity: O(n) [for the above two algorithms]

Prepared by Somenath Sengupta Page 7 of 7

You might also like