algo_074
Problem Statement
Problem Statement
Write an algorithm to delete a node at a given position in a singly linked list.
Algorithm
procedure DeleteNodeAtPosition(head, POS)
begin
if head = NULL then
print("List is empty / Underflow")
return(NULL)
endif
if POS = 1 then
temp ← head
head ← next(head)
return(head)
endif
ptr ← head
for i ← 1 to POS - 2 do
if ptr = NULL then
print("Invalid position")
return(head)
endif
ptr ← next(ptr)
endfor
if ptr = NULL or next(ptr) = NULL then
print("Invalid position")
return(head)
endif
temp ← next(ptr)
next(ptr) ← next(temp)
return(head)
end procedure1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32