zoukankan      html  css  js  c++  java
  • leetcode_234. 回文链表

    请判断一个链表是否为回文链表。
    
    示例 1:
    
    输入: 1->2
    输出: false
    示例 2:
    
    输入: 1->2->2->1
    输出: true
    进阶:
    你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/palindrome-linked-list
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def isPalindrome(self, head: ListNode) -> bool:
            ls=[]
            while(head):
                ls.append(head.val)
                head=head.next
    
            start=0
            end=len(ls)-1
            while(start<end):
                if ls[start]!=ls[end]:
                    return False
                start+=1
                end-=1
            return True
    
  • 相关阅读:
    返回一个整数数组中最大子数组的和2
    RT-Thread之自动初始化
    Git
    基于STM32的FreeRTOS移植
    RT-Thread之debug使用
    大数的进制转换
    uva-10110
    UVA-10061
    算法训练Maze
    森林变树
  • 原文地址:https://www.cnblogs.com/hqzxwm/p/14151462.html
Copyright © 2011-2022 走看看