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.

给定一个头结点为 head 的非空单链表,返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点

Example 1:
在这里插入图片描述
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.

示例 1:

输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
说明:返回的结点值为 3 。 (序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.

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.

示例 2:

输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
说明:由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

Constraints:

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

提示:

  • 列表中的结点数在[1,100]范围内。1 <= Node.val <= 100
  • 1 <= Node.val <= 100

解题思路:

方法一:数组

我们可以对链表进行遍历,同时将遍历到的元素依次放入数组 a中。当链表以及数组的长度也为 n,对应的中间节点即为 a[n/2]。

Python代码

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        a = [head]
        while a[-1].next:
            a.append(a[-1].next)
        return a[len(a) // 2]

Java代码

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode[] a = new ListNode[100];
        int n = 0;
        while (head != null) {
            a[n++] = head;
            head = head.next;
        }
        return a[n / 2];
    }
}

C++代码

class Solution {
public:
  ListNode* middleNode(ListNode* head) {
	vector<ListNode*> a = { head };
	while (a.back()->next != NULL){
		a.push_back(a.back()->next);
    }
	return a[a.size() / 2];
    }
};

复杂度分析

  • 时间复杂度:O(N),其中 N 是给定链表中的结点数目。
  • 空间复杂度:O(N),即数组 A 用去的空间。

方法二:双指针

我们使用双指针定义快慢指针slow和fast,快慢指针都从链表的第一个节点开始移动,慢指针一次走一步,快指针一次走两步,当快指针到达末尾返回慢指针的位置。

Python代码

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        slow, fast = head, head # slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow

Java代码

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

C++代码

class Solution {
public:
    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;
    }
};

复杂度分析

  • 时间复杂度:O(N),其中 N 是给定链表的结点数目。
  • 空间复杂度:O(1),只存放 slow 和 fast 两个指针。
  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值