Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you traverse a linked list in O(n^0.5)?

This is an interview question of Apple. I have found no convincing argument in support or against it yet.

like image 506
Pritpal Avatar asked Jun 24 '11 18:06

Pritpal


People also ask

What is traversal in linked lists?

Traversing a Linked List. A linked list is a linear data structure that needs to be traversed starting from the head node until the end of the list. Unlike arrays, where random access is possible, linked list requires access to its nodes through sequential traversal.

How do you traverse a linked list in Python?

Traversing a Linked List. Singly linked lists can be traversed in only forward direction starting form the first data element. We simply print the value of the next data element by assigning the pointer of the next node to the current data element.


1 Answers

Traversal more efficient than O(n) is not possible, since "traversal" requires accessing each node in turn.

It is possible to make random access faster than O(n) though, by maintaining a second linked list that retains links to intermediate nodes; insertion, deletion, and appending cost will go up though, due to the increased complexity of maintenance of the second list.

like image 50
Ignacio Vazquez-Abrams Avatar answered Sep 27 '22 22:09

Ignacio Vazquez-Abrams