Middle of Linked List on GFG: Fast and Slow Pointer Approach

𝗦𝗹𝗼𝘄 𝗽𝗼𝗶𝗻𝘁𝗲𝗿. 𝗙𝗮𝘀𝘁 𝗽𝗼𝗶𝗻𝘁𝗲𝗿. 𝗢𝗻𝗲 𝗲𝗻𝗱𝘀 𝗮𝘁 𝘁𝗵𝗲 𝗺𝗶𝗱𝗱𝗹𝗲. Today's problem: Middle of a Linked List on GFG 🟢 — an easy one, but worth understanding properly. Slow moves 1 step, fast moves 2. When fast hits the end, slow is at the middle — because fast covers exactly 2x the distance. The thing worth noting: while fast and fast.next aren't the same check. fast handles an empty list or fast landing on None mid-traversal. fast.next prevents fast.next.next from crashing when fast is on the last node. Same line, two different failure cases. ✅ 1115/1115 | First attempt | O(n) time, O(1) space Day 14 of #1000DaysOfLearning #DSA #Python #GFG #LearningInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories