Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Categorize even and odd nodes in a Linked List

Medium Acceptance 49.80% Points 30.00

Given a link list, modify the list such that all the even numbers appear before all the odd numbers in the modified list. The order of appearance of numbers within each segregation should be the same as that in the original list.

NOTE: Don't create a new linked list, instead rearrange the provided one.

Examples
Example 1

Example 1

Input: Linked List: 1 -> 3 -> 5 -> 7 -> 2 -> 4 -> 6 -> 8

Output: 2 -> 4 -> 6 -> 8 -> 1 -> 3 -> 5 -> 7

Explanation: Even numbers (2, 4, 6, 8) appear first, followed by odd numbers (1, 3, 5, 7).

Example 2

Example 2

Input: Linked List: 10 -> 21 -> 32 -> 43 -> 54 -> 65

Output: 10 -> 32 -> 54 -> 21 -> 43 -> 65

Explanation: Even numbers (10, 32, 54) appear first, followed by odd numbers (21, 43, 65).

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1 <= size of linked list <= 10^5
  • 1 <= Each element of the list <= 10^5
Companies
Amazon Visa Oracle Goldman Sachs Paypal
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions