lc 876.链表的中间结点【***链表 - 快慢指针】

it2023-10-23  71

官方题解https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* middleNode(ListNode* head) { ListNode *fast=head, *slow=head; while(fast && fast->next){ slow=slow->next; fast=fast->next->next; } return slow; } };

 

最新回复(0)