Partitioning a Linked List on LeetCode

🚀 Day 49 of #100DaysOfCode Solved 86. Partition List on LeetCode 🔗 🧠 Key Insight: We need to rearrange a linked list such that: 🔹All nodes < x come before nodes ≥ x 🔹While maintaining the original relative order This is a classic linked list partitioning problem. ⚙️ Approach: 1️⃣ Create two dummy lists: 🔹small → nodes with values < x 🔹large → nodes with values ≥ x 2️⃣ Traverse the original list: 🔹Append each node to the appropriate list 3️⃣ Connect both lists: 🔹small → large 4️⃣ Return the head of the new list 🎯 This ensures: 🔹Stable ordering 🔹Clean and efficient separation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (no extra nodes, just pointers) #100DaysOfCode #LeetCode #DSA #LinkedList #Algorithms #Java #CodingPractice #InterviewPrep

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories