CSL 201 Data Structures
Quiz 1 45 minutes
Name:
Roll Number:
1. Describe in detail an algorithm for reversing a singly linked list L by only redirecting the existing
next links (the number of additional nodes created is a constant). (8 points)
Solution:
1
2
3
(1) current = null ;
predecessor = null ;
successor = null ;
4
5
6
7
8
9
10
11
12
13
( 2 ) c u r r e n t=head
( 3 ) w h i l e ( c u r r e n t != n u l l )
{
s u c c e s s o r = c u r r e n t . next ;
c u r r e n t . next = p r e d e c e s s o r ;
predecessor = current ;
current = successor ;
}
( 4 ) head = p r e d e c e s s o r ;
2. Let B be an array of size n ≥ 6 containing integers from 1 to n − 5 inclusive, five of which are
repeated. Describe an algorithm for finding the five integers in B that are repeated. (3 points)
Solution:
1
(1) Sort the array in ascending or descending order .
2
3
4
5
6
( 2 ) f o r i =0 t o n−2
i f (B [ i ]==B [ i +1])
Print b [ i ] ;
end f o r
7
3. Give an algorithm for finding the second-to-last node in a singly linked list in which the last node
is indicated by a null next reference.(2 points)
Solution:
1
2
3
4
5
6
7
8
9
10
11
Element s e c o n d t o l a s t ( head ) {
node<E> temp = head ;
i f ( temp = n u l l )
return null ;
i f ( temp . next = n u l l )
return null ;
w h i l e ( ( temp . next ) . next != n u l l ) {
temp=temp . next ;
}
r e t u r n temp . data ; // r e t u r n i n g t h e e l e m e n t s t o r e d i n t h e s e c o n d l a s t node .
}
1
4. What is the result of trying to compile and run the following program? (2 points)
1
2
3
4
5
6
7
8
9
10
11
p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
int [ ] a = {1};
Test t = new Test ( ) ;
t . increment ( a ) ;
System . out . p r i n t l n ( a [ a . l e n g t h − 1 ] ) ;
}
void increment ( i n t [ ] i ) {
i [ i . l e n g t h − 1]++;
}
}
Output: 2
Reason: The function will be called by reference and no copy of the parameter that is passed is created.
So whatever changes are made it affects the original variable.
2
© Copyright 2026 Paperzz