Reverse Linked List Solution

We are going to share the Reverse Linked List Solution today. If you are applying to any software company 30% of the time you are going to have this question to solve.

What is Linked List?

Linked lists are sequential data structures that connect nodes. Each node consists of two parts: A pointer with the memory location of the following node in the list is also included.

Nodes in a singly linked list only keep a single pointer to the following node in the chain. On the other hand, a node in a doubly linked list contains two pointers: one to the node that comes after it in the list and one to the node that comes before it. In contrast to a doubly linked list, a simple linked list is unidirectional. The tail node’s pointer is null in a single linked list.

The following are some terminology related to linked lists:

Reverse Linked List Solution
Linked List
  • Node: The item in the list that consists of two parts: an element of data and a pointer.
  • Next: The node in a series that follows a certain list node.
  • Head: A linked list’s top node
  • Tail: It is last node of linked list.
  • Head pointer: The memory location in the head node that points to the list’s top node

Linked lists may rank after arrays as the second most popular data structure. These make it easy to add and remove nodes fast. In addition to other abstract data types, they may be used to build stacks and queues.

Reverse Linked List Solution in Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
# = next
class Solution(object):
    def reverseList(self, head):
        :type head: ListNode
        :rtype: ListNode
        current = head
        previous = None
        while current:
            next =
   = previous
            previous = current
            current = next
        head = previous
        return head

For Reverse Linked List Solution in other languages visit GeeksforGeeks


Data Structures related posts visit HERE

Python-related posts Visit HERE

C/C++ related posts Visit HERE

Databases related posts Visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge