LeetCode | 876. Middle of the Linked List

.

题目

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

Example 1:

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.

Example 2:

Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.

Constraints:

  • The number of nodes in the list is in the range [1, 100].
  • 1 <= Node.val <= 100

.

代码

/**
 * 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) {
    
    
        int numNode = 0;
        ListNode* p;
        for(p = head; p != NULL; )
        {
    
    
            numNode++;
            p = p->next;
        }
        int i = 0;
        for(p = head; i < numNode / 2 && p != NULL; i++)
        {
    
    
            p = p->next;
        }
        return p;
    }
};

.

又是一次AC ^ ___________________________________ ^

又到了熟悉的“转眼又过去了一个月”。
这个六月由紧张、期待构成。
突然出现了一个非常诱惑的选择,可以开始科研生涯。
这也是我曾经梦想的目标之一。
经历了10多天的纠结、迟疑、决心、思考,
目前我暂缓这个选择。

接着是梦华录的美好回忆。
伴着我纠结犹豫的时期,梦华录缓解了我的紧张情绪。
这次也很感谢支持我陪伴着我的家人和朋友。
同样感谢刘亦菲和陈晓 :)
虽然他们可能不知道,
但这部剧真的太棒了,好久没看到刘亦菲的电视剧。
希望她一直如此。
不骄不躁,一切都无法影响她的样子,平静地走向理想的岛屿。

接着就是忙碌的工作和学习,
连续将近一个月的熬夜,
我现在该休息停笔了。
过去一个月有好多快乐的瞬间,
解封之后可以去小区附近的花园散步,
每天看看这片城市中心的绿地,
真让人抽离,
让人打开心胸。

感谢六月,
接下来就是,最喜欢的七月啦!
当然我也很喜欢其他月份啦!哈哈哈哈哈哈
再见,六月;你好,七月~

猜你喜欢

转载自blog.csdn.net/iLOVEJohnny/article/details/125550438
今日推荐