当前位置 博文首页 > OIqng的博客:876. (Middle of the Linked List)链表的中间结

    OIqng的博客:876. (Middle of the Linked List)链表的中间结

    作者:[db:作者] 时间:2021-09-04 15:40

    题目:

    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 两个指针。
    cs