Grokking Microsoft Coding Interview
Vote

0% completed

​

Reverse a LinkedList (easy)

Problem Statement

Try it yourself

Problem Statement

Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.

Image

Constraints:

  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000

Try it yourself

Try solving this question here:

Python3
Python3

. . . .
A

Anonymous

· 2 years ago

#class Node: #  def __init__(self, value, next=None): #    self.val = value #    self.next = next class Solution:   def reverse(self, head):     node = head     prev = None         while node.next:       next_node = node.next       node.next = prev       prev = node       node = next_node         node.next = prev     return node
A

Anonymous

· 2 years ago

The explanations for all the algorithm are poor

A

Anonymous

· 3 years ago

class Solution: def reverse(self, head): prev = None curr = head while curr: temp = curr.next curr.next = prev prev = curr curr = temp return prev

Reading Progress

0%


Vote for new content

On This Page

Problem Statement

Try it yourself