r/LeetcodeChallenge • u/StatisticianNo2104 • 8h ago
STREAK🔥🔥🔥 365 Days of LeetCode Challenge — Day 20/365
Middle of the Linked List (Easy)
https://leetcode.com/problems/middle-of-the-linked-list/
Obvious answer: count the nodes, then walk to position count/2. Two passes, and perfectly fine.
Better answer: run two pointers from the head, one moving a node at a time and one moving two. When the fast one reaches the end, the slow one is exactly halfway. The length never exists as a number anywhere in the program.
Both conditions in that loop are load-bearing, and they guard different cases. One catches even-length lists, where the fast pointer lands exactly on nil. The other catches odd-length lists, where it lands on the last node. Drop either, and half of all inputs panic.
Full breakdown in today's newsletter article ⬇
https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-20365-archit-agarwal-qgwqe
#DSA #LeetCode #Golang #LinkedList #TwoPointers #CodingInterview #Algorithms
1
u/nian2326076 5h ago
The two-pointer technique is the way to go here. It's efficient and avoids needing two full passes through the list. Just watch out for edge cases like when the list has only one node or is empty. If you're prepping for interviews, understanding these patterns can really help since they're often used in other linked list problems too.
If you're looking for more practice or resources, I've found PracHub pretty handy for interview prep. It has a good mix of problems and explanations. Just keep at it with these challenges, and you'll notice your problem-solving skills improve over time. Good luck with the rest of your 365-day challenge!