206. Reverse Linked List

Prompt

Given the head of a singly linked list, we want to return that same linked list but in reversed order.

Approach

The main challenge here how do we reverse a singly linked list? As opposed to a doubly linked list that has an additional pointer commonly called prev which allows you to access the previous node, a singly linked list only has a single pointer that points to the memory address or reference of another node (commonly called next).

prev = None
cur = head
while cur != None:
temp = cur.next
prev = cur
cur = temp
return prev

Code

Python

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Kim

John Kim

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com