Leetcode-206 :-Reverse Linked List

Given the head of a singly linked list, reverse the list, and return the reversed list.

Image of reversed linked list.

A easy category linked list problem where we need to reverse the Linked list.For this there are two method-iterative and recurrssive method.Here we will apply the iterative method. For this method we will need to use to variable “prev” and “next_current”.Let’s move toward the code.

def reverseList(self, head: ListNode) -> ListNode:
while current:
return prev

First we will give initial value of Null to prev variable and save head of Linked list in variable “current”. The we will iterate the Linked list using a while loop.we will put the next value of current i.e head in the variable next_current and point the next of current to prev variable as it holds the null value.The change the value of prev to current and move current to next_current as it hold the link to next element in Linked List and once the loop exit the last element will be made head and we will return prev which holds the reverse list.

“Difficult things aren’t easy but they are worth it”.
Interesting quote and it fits well for Data structure which always seems difficult but once learning start it ain’t that difficult. Move out of Comfort zone.Keep learning and Keep hustling…(cheers:PN)

Work in progress