Question
Which of the following operations is most efficient in a
singly linked list compared to an array?Solution
In a singly linked list, deleting the first element is very efficient and takes O(1) time. This is because the head pointer of the linked list can directly reference the next node, bypassing the first node. No traversal or shifting of elements is required, unlike an array, where deletion involves shifting all subsequent elements, making it O(n) The pointer manipulation in a linked list ensures this operation is constant time, making it highly advantageous over arrays for scenarios involving frequent deletion of the first element. Why Other Options Are Incorrect · Option 1 (Accessing the middle element): In a singly linked list, accessing any element requires traversing from the head node to the desired position, making it O(n) In contrast, an array supports direct access using an index, making it O(1) · Option 3 (Accessing the last element): Accessing the last element in a singly linked list requires traversing through all nodes from the head, making it O(n) In an array, accessing the last element is an O(1) operation because the index of the last element is directly available. · Option 4 (Inserting an element at a specific position): To insert at a specific position, a singly linked list requires traversal up to that position, which is O(n) In an array, insertion requires shifting elements, also O(n), but for specific use cases, arrays might be preferable for direct indexing. · Option 5 (Searching for an element): Searching is linear in a singly linked list (O(n) , as each node must be checked. In arrays, searching can also be O(n) , but if the array is sorted, binary search can reduce complexity to O(log n)
A cyclist covers a distance of 14 km in 25 minutes. How much distance will he cover in 18 minutes (in km)?
The speed ratio of Y and Z is 5 : 4. If Y increases his speed by 25%, he can cover 300 km in 4 hours. How long will Z take to travel 192 km?
P' travelled from Delhi to Agra, covering a total distance of 396 km. For the initial three hours, he maintained an average speed of ‘a’ km/h. After...
Two cars, labeled 'A' and 'B', simultaneously depart from the same starting point, traveling in the same direction. Car 'A' moves at a speed of 15 km/h,...
Two trains of lengths 120 m and 150 m are moving in opposite directions at speeds of 45 km/h and 30 km/h, respectively. How much time will they take to ...
A car is traveling at a speed of 144 km/hr. The car's tires have a diameter of 70 m. How long will it take for the car to complete 12 revolutions of its...
The speed of an electric car is 100 km/h, taking 7 hours to traverse a distance. A bus takes 5 hours to cover the same distance. If the electric car's s...
- A security guard notices a suspect 120 meters ahead and starts chasing him. The suspect runs at a speed of 18 m/sec, while the security guard runs at 22 m/...
- ‘X’, ‘Y’ and ‘Z’ can complete one lap around a circular track in 10 seconds, 15 seconds, and 20 seconds respectively. If they all start from th...
- A bus leaves town 'X' at 9:30 AM heading towards 'Y' at a speed of 50 km/h. A van starts from the same place at 11:00 AM towards 'Y' at 70 km/h. At what ti...