LeetCode—876. Middle of the Linked List


//url:https://leetcode.com/problems/middle-of-the-linked-list/description/

/**
 * 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) {
        vector list;
        ListNode* tmp=head;
        while(tmp->next){
            list.push_back(tmp);
            tmp=tmp->next;
        }
        list.push_back(tmp);
        return list[list.size()/2];
        
    }
};



发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注