Finding the middle node of a singly linked list || LeetCode 876

preview_player
Показать описание


This is inspired by the Cracking the Coding Interview book by Gayle Laakmann McDowell
This is based on chapter 2 Linked Lists, the runner approach

In this solution we approach the problem using the slow pointer and fast pointer concept. Another way to achieve the same is by first finding the length of the linked list and then finding the middle element which will also take O(n) time though we have to traverse the list twice.

Рекомендации по теме
Комментарии
Автор

Good approach! eye opener

One thing to note, you don't actually need to declare a slow pointer. It's still a 2 pointer approach but instead of using slow, you can use the head pointer itself. head = head.next. Then at the end, return head.

Makwayne