码迷,mamicode.com
首页 > 其他好文 > 详细

876 链表中间节点

时间:2019-01-12 18:30:27      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:null   简洁   pre   slow   ext   自己   head   nullptr   lis   

  ListNode* middleNode(ListNode* head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast != NULL && fast->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

自己写的不够简洁

ListNode *middleNode(ListNode *head) {
    if (head == nullptr || head->next == nullptr)
        return head;
    ListNode *slow = head, *fast = head->next;
    while (fast->next != nullptr) {
        fast = fast->next;
        slow = slow->next;
        if (fast->next == nullptr)
            return slow;
        fast = fast->next;
    }
    return slow->next;
}

876 链表中间节点

标签:null   简洁   pre   slow   ext   自己   head   nullptr   lis   

原文地址:https://www.cnblogs.com/INnoVationv2/p/10260528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!