给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
样例
样例 1:
输入:1->2->3->4->5->null
输出:3->4->5->null
样例 2:
输入:1->2->3->4->5->6->null
输出:4->5->6->null
注意事项
The number of nodes in the given list will be between 1 and 100.
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: the head node
* @return: the middle node
*/
ListNode * middleNode(ListNode * head) {
// write your code here.
ListNode *p=new ListNode(0);
p->next=head;
ListNode*fast=p;
ListNode*slow=p;
while(fast)
{
fast=fast->next;
if(fast) fast=fast->next;
slow=slow->next;
}
return slow;
}
};