Leetcode 876 Middle of the Linked List

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010821666/article/details/82749544

Leetcode 876 Middle of the Linked List

struct ListNode
{
    int val;
    ListNode* next;
    ListNode(int x):val(x),next(NULL){}
};
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        if(!head || !head->next)
            return head;
        int count = 0;
        ListNode* node = head;
        while(head){
            head = head->next;
            count ++;
        }
        for(int i = 0;i < count/2;++i){
            node = node->next;
        }
        //if(count % 2 == 0)
        //    node = node->next;
        return node;
    }
};

猜你喜欢

转载自blog.csdn.net/u010821666/article/details/82749544
今日推荐