Linked List Manipulation: Move Last Node to Front

🌟 Day 73 of #100DaysOfCode 🌟 🔗 Move the Last Node to the Front – A Simple Yet Powerful Linked List Pattern! 🔹 What I Solved Today’s challenge focused on an elegant linked list manipulation where the last node is moved to the front. This problem may look simple, but it strengthens understanding of pointer traversal and last-node handling — techniques that are frequently tested in interviews. 🧩 Problem: Move Last Node to Front of Linked List 💡 Given: A linked list such as: 2 → 5 → 6 → 2 → 1 💥 Goal: Take the last node of the list and move it to the front, returning the modified list — all in O(n) time and O(1) space. 📌 Example 1 Input: 2 → 5 → 6 → 2 → 1 Output: 1 → 2 → 5 → 6 → 2 📌 Example 2 Input: 2 Output: 2 (Only one node → no change) 🧠 Concepts Used ➡️ Pointer Traversal Traverse the list to find the last and second-last nodes. ➡️ Re-linking Pointers Detach the last node and connect it before the head. ➡️ Edge Case Handling If the list has only one node, return it as-is. ➡️ Efficient Logic Single traversal → O(n) time No extra memory → O(1) space ⚙️ Approach 1️⃣ Traverse to the second-last node 2️⃣ Separate the last node 3️⃣ Point the last node to the current head 4️⃣ Update head to the last node 5️⃣ Return the modified list 🚀 Learning This problem helped improve my understanding of: ✔️ Boundary cases in linked lists ✔️ Precise pointer manipulation ✔️ Clean and optimal transformations ✔️ Writing space-efficient code ✔️ Thinking in terms of node connections #CodingLife #Programmer #LinkedList #GeeksForGeeks #JavaDeveloper #StudentCoder #ProblemSolving

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories