Reverse a Singly Linked List
Updated
Problem statement
You are given the head head of a finite, acyclic singly linked list. Reverse the direction of every next pointer in place and return the new head.
The returned list must contain exactly the original nodes and values in reverse order. Return null when the input list is empty.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [1,2,3,4,5]return = [5,4,3,2,1]Moved or new
Every link is reversed, making 5 the new head and 1 the new tail.
Example 2
head = []return = []An empty list remains empty.
Example 3
head = [7]return = [7]A one-node list is already reversed.
Constraints
0 <= number of nodes <= 100000.-1000000000 <= node.val <= 1000000000.- The input list is finite and acyclic.
More GEP interview questions
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions
- Reverse a Singly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Reverse Linked ListThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Detect a Linked-List CycleGoogle · ONSITE INTERVIEWGoogle interview questions
- Detect a Cycle in a Linked ListPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions