给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
数据范围: 链表节点数
,链表中每个节点的值满足
class Solution: def isPail(self , head: ListNode) -> bool: if not head&nbs***bsp;not head.next: return True # write code here # 1.找到中点 fast, slow = head, head slow_pre = head while fast and fast.next: fast = fast.next.next slow_pre = slow slow = slow.next # 2.slow 会在中间靠右的地方 # [1, 2, 2, 1] => slow 右边的 2 # [1, 2, 3, 2, 1] => slow 在正中间 # 断开链表 slow_pre.next = None # 2.翻转链表 def reverse(node): prev = None while node: tmp = node.next node.next = prev prev = node node = tmp return prev slow = reverse(slow) # 因为 head 会少一些 while head: if slow.val != head.val: return False slow = slow.next head = head.next return True
class Solution: def reverse(self, head): pre = None cur = head while cur: temp = cur.next cur.next = pre pre = cur cur = temp return pre def isPail(self , head: ListNode) -> bool: if not head: return True slow, fast = head, head while fast and fast.next: fast = fast.next.next slow = slow.next slow = self.reverse(slow) fast = head while slow: if slow.val != fast.val: return False fast = fast.next slow = slow.next return True
# 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: ListNode) -> bool: # write code here ## 空值 和 单值 均是 True if not head&nbs***bsp;not head.next: return True l = [] ll = [] cur = head while cur: l.append(cur.val) # ll.append(cur.val) cur = cur.next ll = l[:] ## 使用这个会快一些 l.reverse() if ll == l: return True else: return False
# 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: ListNode) -> bool: # write code here if head == None: return True p = head tmp = [] while p != None: tmp.append(p.val) p = p.next if tmp == tmp[::-1]: return True else: return False
class Solution: def isPail(self , head: ListNode) -> bool: # write code here slow, fast = head, head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next # 寻找中点 if fast.next: mid = slow.next else: mid = slow # 从mid开始翻转链表 last = None while mid: next = mid.next mid.next = last last = mid mid = next # 开始对比回文 while last: if last.val != head.val: return False last = last.next head = head.next return True
class Solution: def isPail(self , head: ListNode) -> bool: low=head fast=head dummy=head res=[] while fast and fast.next: if fast.next.next: res.append(low.val) fast=fast.next.next low=low.next else: res.append(low.val) break low=low.next while low and low.next: if res.pop()==low.val: low=low.next else: return False return True
class Solution: def isPail(self , head: ListNode) -> bool: # write code here pos= [] neg = [] while head: pos.append(head.val) head = head.next st = True for i in range(len(pos)//2): if pos[i] != pos[-i-1]: st = False return st直接用堆栈[::-1]是会超时的,所以想办法优化这个的判断,其实只用看前一半就好,偶数长度,只用判断pos[x] 和pos[-x-1]是否相同,然后判断一般就好,奇数最中间那个不用管,剩下的类似
class Solution: def isPail(self , head: ListNode) -> bool: # write code here a=[] if not head :return True p=head while p: a.append(p.val) p=p.next for i in range(len(a)): if a[i]!=a[-i-1]: return False return True
# 栈+双指针 class Solution: def isPail(self , head ): # write code here stack = [head.val] slow = head fast = head while(fast.next and fast.next.next): slow = slow.next fast = fast.next.next stack.append(slow.val) if fast.next is None: stack.pop() while(stack!=[] and slow.next): slow = slow.next if stack.pop()!=slow.val: return False return True
# 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 valu = [] while head: valu.append(head.val) head = head.next for i in range(len(valu)//2+1): if valu[i] != valu[len(valu)-1-i]: return 0 return 1
class Solution: def isPail(self , head ): # write code here if not head: return head slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next pre = None while slow: temp = slow.next slow.next = pre pre = slow slow = temp while head and pre: if head.val == pre.val: head = head.next pre = pre.next else: return False return True