Rearrange a Linked List from Alternating Ends
Updated
Problem statement
Rearrange a singly linked list L0 → L1 → … → Ln into L0 → Ln → L1 → Ln-1 → ….
Reuse the existing nodes and return the head. Do not change node values.
Function
rearrangeList(head: ListNode) → ListNodeExamples
Example 1
head = [1,2,3,4]return = [1,4,2,3]Moved or new
The alternating order is 1,4,2,3.
Example 2
head = [1,2,3,4,5]return = [1,5,2,4,3]Moved or new
The middle node remains last.
Example 3
head = []return = []An empty list stays empty.
Constraints
- The list contains at most
100000nodes. - Node values fit in a signed 32-bit integer.
More Concentric AI interview questions
Similar questions at other companies
- Remove Nth Node From End of ListDruva · ONSITE INTERVIEW
- Find the Entry of a Linked List CycleQualys · ONSITE INTERVIEWQualys interview questions
- Detect a Linked-List CyclePaytm · ONSITE INTERVIEWPaytm interview questions
- Detect a Linked-List CycleCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Palindrome Linked List with RestorationMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Linked List Cycle Entry NodeGoogle · ONSITE INTERVIEWGoogle interview questions
- Detect and Break a Linked-List CycleAmazon · ONSITE INTERVIEW
- Odd-Even Linked ListBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Find the Entry of a Linked List CyclePeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Remove a Cycle from a Singly Linked ListExpedia · ONSITE INTERVIEWExpedia interview questions