LeetCode 876 Middle of the Linked List 解题报告
題目要求
Given a non-empty, singly?linked list with head node?head, return?a?middle node of linked list.
If there are two middle nodes, return the second middle node.
題目分析及思路
題目給出一個非空單鏈表,要求返回鏈表單的中間結點。可以將鏈表中的結點保存在list中,直接獲取中間結點的索引即可。
python代碼
# Definition for singly-linked list.
# class ListNode:
#? ? ?def __init__(self, x):
#? ? ? ? ?self.val = x
#? ? ? ? ?self.next = None
class Solution:
? ? def middleNode(self, head: 'ListNode') -> 'ListNode':
? ? ? ? l = [head]
? ? ? ? while l[-1].next:
? ? ? ? ? ? l.append(l[-1].next)
? ? ? ? return l[len(l) // 2]
? ? ? ??
轉載于:https://www.cnblogs.com/yao1996/p/10360948.html
總結
以上是生活随笔為你收集整理的LeetCode 876 Middle of the Linked List 解题报告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux安装MariaDB(Mysql
- 下一篇: nginx正向代理 反向代理