S2 icon indicating copy to clipboard operation
S2 copied to clipboard

0876. Middle of the Linked List | LeetCode Cookbook

Open halfrost opened this issue 4 years ago • 1 comments

https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0876.Middle-of-the-Linked-List/

halfrost avatar Apr 29 '21 15:04 halfrost

用官方的推荐的方法可以省略了后面的二次遍历更好一点。只是边界略不好理解一点

func middleNode(head *ListNode) *ListNode {
    slow,fast := head,head
    for fast != nil && fast.Next != nil {
        slow = slow.Next
        fast= fast.Next.Next
    }
    return slow
}

baso4 avatar Mar 07 '22 10:03 baso4