Medium Data Structures Linked Lists
How do you reverse a linked list iteratively and recursively?
Answer
Iteratively: use three pointers (prev, curr, next). In each step, save curr.next, point curr.next to prev, move prev to curr, move curr to saved next. Continue until curr is null; prev is the new head. Recursively: base case returns head when null or single node. Recursively reverse rest, then set head.next.next = head and head.next = null. Both are O(n) time; iterative is O(1) space, recursive is O(n) stack space.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Algorithm Developer