234. 回文链表

    科技2022-07-21  116

    请判断一个链表是否为回文链表。 示例 1: 输入: 1->2 输出: false

    思路

    把链表里面的数值插入到一个列表中。 之后一边从左往右看,一边由右往左看,看两边的数值是否相等。

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ l = [] while head != None: l.append(head.val) head = head.next i,j = 0,len(l)-1 while i <j: if l[i]!= l[j]: return False i = i+1 j = j-1 return True

    Processed: 0.011, SQL: 8