Grokking the Coding Interview: Patterns for Coding Questions
0% completed
Reverse a LinkedList (easy)
Problem Statement
Given the head of a Singly LinkedList, reverse the LinkedList. Write a function to return the new head of the reversed LinkedList.
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:
.....
.....
.....
Like the course? Get enrolled and start learning!
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%