题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
http://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
思路:
1.将链表转成列表处理
2.使用双指针大法
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head ListNode类 the head
# @return bool布尔型
#
class Solution:
def isPail(self , head ):
# write code here
nums=[]
p=head
#将链表变成列表
while p:
nums.append(p.val)
p=p.next
n=len(nums)
l=0
r=n-1
while l<r:
if nums[l]!=nums[r]:
return False
l+=1
r-=1
return True
查看8道真题和解析