Reverse Linked-Node Order by Iteration and Recursion

Quick Overview

Reverse original linked-list links iteratively and recursively with correct suffix preservation and space accounting.

Reverse Linked-Node Order by Iteration and Recursion

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Reverse a singly linked list and return the reversed chain's head. Preserve the supplied ListNode pointer interface with val and next fields; example brackets display chain values only. ```cpp ListNode* reverseList(ListNode* head); ``` ### Examples ```text Input chain: [6,-1,4] Output chain: [4,-1,6] ``` ```text Input chain: [] Output chain: [] ``` ### Constraints - There are between 0 and 5000 nodes. - Values lie between -5000 and 5000. ### Optional follow-up Provide both an iterative and a recursive reversal.

Overview: Reverse original linked-list links iteratively and recursively with correct suffix preservation and space accounting.

Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Reverse a singly linked list and return the reversed chain's head. Preserve the supplied ListNode pointer interface with val and next fields; example brackets display chain values only.

ListNode* reverseList(ListNode* head);

Examples

Input chain: [6,-1,4]
Output chain: [4,-1,6]
Input chain: []
Output chain: []

Constraints

  • There are between 0 and 5000 nodes.
  • Values lie between -5000 and 5000.

Optional follow-up

Provide both an iterative and a recursive reversal.