# Is it possible to reverse a linked list in less than O(n)?

## Introduction

A Linked List is a collection of nodes-objects containing two fields-the data stored and the pointer having the address of the next node in memory. The first node is usually called the ‘Head’, where the linked list is traversed from.

## Can we reverse a linked list in less than O(n) time?

Linked list operations usually require reversing them, and working with single and double linked lists in programs usually needs the least time possible for operations. Hence the question here is: Can we reverse a linked list in less than O(n) time?

### The solution

#### Singly-linked list

The structure of a linked list is like this:

A singly linked list can be reversed using iteration and recursion, but the least possible time doing that is O(n). Every node in the linked list will be visited once to reverse it, even if we transverse the list only once. For example, for the above shown linked list to be reversed, all nodes—starting from 19 to 52, 23 and 12, respectively—will be visited when we navigate through the list. Recursion and iteration will take linear time, meaning the order will not go any lower than O(n) in either of the methods used to reverse-using extra space or simply modifying and reversing the pointers.

Let’s consider the memory-efficient doubly linked list now.

#### Doubly linked list

A doubly linked list has nodes with both head and tail pointers, and it looks like this:

If we traverse such a linked list from the pointer to the node, that will be the reverse order of the linked list. Hence for doubly-linked lists, we can swap the head and tail pointers, which takes constant time. Now, this process will take O(1) time, but to traverse in the forward direction, we will need to use the prev pointer and to move in the reverse direction, we will need the next pointer. This is bound to cause a lot of confusion and may not be valid.

**Therefore, we cannot reverse a linked list in less than O(n) time.**

## Frequently Asked Questions

**Q1. **What are linked lists used for?

**Ans.** Linked lists are linear collections of data elements stored randomly in the memory. They allow efficient insertion and deletion and can implement queues, stacks and other abstract data types.

**Q2.** What is the advantage of linked lists over arrays?

**Ans.** In a linked list, the elements can be inserted or deleted very easily. There is no reallocation or major reorganization of the data structure because its elements might not be stored contiguously. Restructuring arrays, on the other hand, at run-time will be much more complex.

**Q3.** What is the limitation of linked lists?

**Ans. **Linked lists take up more memory than arrays because every node of a linked list has a pointer for the next node, which requires more memory. Traversing nodes might not be very simple in linked lists and accessing any node randomly is not possible.

**Q4.** Why do we need to reverse a linked list?

**Ans. **The node’s next property determines the order in a singly linked list. This could either refer to another node or point to null if it is the last node in the list. Reversing a linked list, therefore, refers to reassigning all the next properties on every node.

## Key takeaways

In this article, we learned about the structure of linked lists, singly and doubly-linked lists and why it is not possible to reverse them in less than O(n). The article uses illustrations to explain the concept better and understand the minimum possible time complexity to grasp the working of linked lists.

You can go to __CodeStudio__ to try and solve more problems for practice. Share this blog with your friends if you found it helpful! Until then, All the best for your future endeavours, and Keep Coding.

Comments

## No comments yet

## Be the first to share what you think