每天一道LeetCode——链表的中间结点

题目链接——链表的中间结点

每天一道LeetCode——链表的中间结点_第1张图片

 

#include
struct ListNode* middleNode(struct ListNode* head)
{
    assert(head);
    struct ListNode* ps = head;
    int i = 0;
    while(ps != NULL)
    {
        ps = ps->next;
        i++;
    }
    int n = i/2;
    int count = 0;
    while(1)
    {
        if(count == n)
        {
            break;
        }
        head = head->next;
        count++;
    }
    return head;
}

你可能感兴趣的:(LeetCode,链表,leetcode,数据结构)