leetcode234.回文链表

461 篇文章 1 订阅

题目:
给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
在这里插入图片描述
解答:

方法一:转换为数组,然后进行判断

# 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:
        #转为数组进行判断
        res=[]
        curnode=head
        while curnode:
            res.append(curnode.val)
            curnode=curnode.next
        m=len(res)
        for i in range(m//2):
            if res[i]!=res[m-1-i]:
                return False
        return True

方法二:快慢指针

pre:慢指针的前一个结点
slow:慢指针
fast:快指针

(1)当fast走到链表末尾时,将链表等分为两部分;若链表结点为偶数个,则后半部分多一个结点。
(2)对链表的后半部分进行翻转;
(3)判断链表的前半部分和翻转后的后半部分,对应结点值是否相等;

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    #对链表进行翻转
    def reverseList(self,head):
        pre=None
        cur=head
        tmp=None #保存cur的下一个结点
        while cur:
            tmp=cur.next
            cur.next=pre
            pre=cur
            cur=tmp
        return pre

    def isPalindrome(self, head: ListNode) -> bool:
        #快慢指针
        pre,slow,fast=head,head,head
        while fast and fast.next:
            #pre为slow的前一个结点
            pre=slow
            slow=slow.next
            fast=fast.next.next
        
        #以slow为结点进行分割,slow属于后半部分
        cur1=head
        pre.next=None
        #对后半部分链表进行翻转
        cur2=self.reverseList(slow)
        
        #比较前半部分链表和后半部分链表
        while cur1:
            if cur1.val!=cur2.val:
                return False
            else:
                cur1=cur1.next
                cur2=cur2.next
        return True

方法三:双端队列

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        #使用双向队列模拟操作
        que=collections.deque()
        cur=head.next
        while cur:
            que.append(cur)
            cur=cur.next
        
        cur=head
        #count为偶数从后面取,count为奇数从前面取
        count=0   
        while que:
            if count%2==0:
                node=que.pop()
            else:
                node=que.popleft()
            cur.next=node
            cur=cur.next
            count+=1
        cur.next=None
        return 

方法四:将链表拆分为两部分,再拼接

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    #对链表进行翻转
    def reverseList(self,head):
        pre=None
        cur=head
        tmp=None #保存cur的下一个结点
        while cur:
            tmp=cur.next
            cur.next=pre
            pre=cur
            cur=tmp
        return pre

    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        #快慢指针
        slow,fast=head,head
        while fast and fast.next:
            slow=slow.next
            fast=fast.next.next
        
        #以slow为结点进行分割,slow属于前半部分
        cur1=head.next
        tmp=slow.next
        slow.next=None
        #对后半部分链表进行翻转
        cur2=self.reverseList(tmp)
        
        count=0
        cur=head
        #拼接前半部分链表和后半部分链表
        #前半部分链表始终比后半部分链表多两个节点
        #若为偶数则多两个节点;若为奇数则多一个节点
        while cur1 and cur2:
            if count%2==0:
                cur.next=cur2
                cur2=cur2.next
            else:
                cur.next=cur1
                cur1=cur1.next
            count+=1
            cur=cur.next             
        if cur1:
            cur.next=cur1
        return 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值