本文主要是介绍[LeetCode] 234. Palindrome Linked List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目内容
https://leetcode-cn.com/problems/palindrome-linked-list/
Given a singly linked list, determine if it is a palindrome.
Example 1:Input: 1->2
Output: false
Example 2:Input: 1->2->2->1
Output: true
题目思路
把链表的数值保存在list中,然后比较。
程序代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution(object):def isPalindrome(self, head):""":type head: ListNode:rtype: bool"""if not head:return Truetmp=[]while head:tmp.append(head.val)head=head.nextreturn tmp[:]==tmp[::-1]
这篇关于[LeetCode] 234. Palindrome Linked List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!