Leetcode daily 20/03/23 链表的中间结点

链表的中间结点

-题目-

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

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

-示例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.

-示例2-

输入:[1,2,3,4,5,6]

输出:此列表中的结点 4 (序列化形式:[4,5,6])

由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

-提示-

给定链表的结点数介于 1 和 100 之间。


-方法1-

  • 遍历节点,把节点放到一个列表里。取中间结点即可。

-ac代码-

class Solution:
    def __init__(self):
        self.node_list = []

    def middleNode(self, head: ListNode) -> ListNode:
        while True:
            self.node_list.append(head)

            if head.next:
                head = head.next
            else:
                break

        return self.node_list[int(len(self.node_list) / 2)]

-复杂度-

  • \(T(n) = O(n)\)
  • \(S(n) = O(n)\)

原文地址:https://www.cnblogs.com/Chunngai/p/12551428.html

时间: 2024-08-29 23:43:55

Leetcode daily 20/03/23 链表的中间结点的相关文章

LeetCode 876——链表的中间结点

1. 题目 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 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.n

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

链表的中间结点 (3.23leetcode每日打卡)

给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 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 =

leetCode 234. Palindrome Linked List 链表

234. Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 题目大意: 判断一个单链表是否为回文链表. 思路: 找到链表中间的节点,将链表从中间分为2部分,右半部分进行链表反向转换,然后左半部分和反转后的右半部分链表进行比较.得出结果. 代码如下: /**  * Defi

【Leetcode解题报告】单链表结点位置调整

Leetcode 206 Reverse Linked List 题目描述 Reverse a singly linked list. 分析与解法 (1) 递归解法 参考代码如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pu

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

找链表的中间结点

//找链表的中间结点 /* 已知单链表L,编写算法找出该链表的中间位置的结点. 思考: 1.一种想法就是从头遍历到尾部,记录长度.随后再次遍历一次,直到长度的二分之一即找到.时间复杂度为O(3n/2) 2.另一种想法:设置快慢指针,快指针一次走两步,慢指针一次走一步,当快指针走到NULL的时候,慢指针的位置就是链表的中间位置 本程序使用第二种想法 */ #include <iostream> #include <string> using namespace std; int L_

Leetcode:Sort List 对单链表归并排序

Sort a linked list in O(n log n) time using constant space complexity. 看到O(n log n)的排序算法,适合单链表的首先想到的就是归并排序 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cla