Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

77. Reorder List

MediumDay 23 of 60Linked List

You are given the head of a singly linked list L0 -> L1 -> ... -> Ln. Rearrange it in place into the order L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ..., alternating one node from the front with one node from the back. You must move nodes, not just swap their values. The function returns nothing.

Example 1
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
Take 1 from the front, 5 from the back, 2 from the front, 4 from the back, then the middle 3.
Constraints
  • 1 <= number of nodes <= 5 * 10^4
  • 1 <= Node.val <= 1000
Hints
Frequently asked at: Amazon, Meta, Microsoft, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min