Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse a Linked List Iteratively

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reverse a Linked List Iteratively. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Resultant project workflows may process items in linked order. Given the head of a singly linked list, reverse the list in place using iteration and return the new head.

Formal Specification

The input is head, either None or a reference to a ListNode with fields val and next. The function must return the new head after reversing every next pointer. Do not create new list nodes. For examples and test cases, linked lists are represented as arrays of values, where [] represents None.

Constraints

  • 0 <= n <= 5 * 10^4
  • -10^4 <= node.val <= 10^4
  • The list contains no cycles
  • Do not allocate new list nodes
  • Use iteration and O(1) auxiliary space

Function Signature

def reverse_linked_list(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output