/**
* 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* one=head;
ListNode* two=head;
while(two->next!=NULL&&two->next->next!=NULL)
{
two=two->next->next;
one=one->next;
}
if(two->next==NULL)
{
return one;
}
else
{
return one->next;
}
}
};
Leetcode——876. 链表的中间结点(快慢指针法)
最新推荐文章于 2022-06-13 17:12:05 发布