双指针 | 辅助栈:力扣面试题 02.06. 回文链表

1、题目描述:

在这里插入图片描述

2、题解:

方法1:辅助栈
思路:

思路:
设置一个栈,先把链表里的结点压到栈里
然后从链表头部遍历:每次出栈,比较,遇到不等时,直接返回False
最后都相等,返回True

Python代码:

# 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:
        #辅助栈
        if not head or not head.next:
            return True
        stack = []
        cur = head
        while cur:
            stack.append(cur)
            cur = cur.next
        while head:
            if head.val != stack.pop().val:
                return False
            head = head.next
        return True

方法2:双指针+辅助栈
思路:

思路:
快慢指针,找到中间位置
设置一个栈,先把链表里中间位置后面的结点压到栈里
然后从链表头部遍历:每次出栈,比较,遇到不等时,直接返回False
最后都相等,返回True

Python代码如下:

# 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:
        #双指针 + 辅助栈
        if not head or not head.next:return True
        slow,fast = head,head
        while fast.next and fast.next.next:
            fast = fast.next.next
            slow = slow.next
        stack = []
        while slow:
            stack.append(slow)
            slow = slow.next
        while stack:
            if head.val != stack.pop().val:
                return False
            head = head.next
        return True

方法3:双指针
思路:

思路:
快慢指针,先找到中间位置(奇数在正中间,偶数在中间的前一个位置),并记录
然后把中间位置后面的逆序
然后用两个指针,开始向中间走,有任意结点不等,就返回False
最后返回True,要把逆序的部分改回来

Python代码如下:

# 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:
        #双指针
        if not head or not head :return True
        #找到中间位置
        slow,fast = head,head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        #开始反转
        second = slow.next #记录右边的链表开头
        slow.next = None
        while second :
            temp = second.next
            second.next = slow
            slow = second
            second = temp
        third = slow #记录反转后的头结点
        #开始判断匹配
        res = True
        first = head
        while first and third:
            if first.val != third.val:
                res = False
                break
            first = first.next
            third = third.next
        #还原链表
        second = slow.next
        slow.next = None
        while second:
            temp = second.next
            second.next = slow
            slow = second
            second = temp
        return res

3、复杂度分析:

方法1:
时间复杂度:O(N)
空间复杂度:O(N)
方法2:
时间复杂度:O(N)
空间复杂度:O(N)
方法3:
时间复杂度:O(N)
空间复杂度:O(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值