(Python) algorithm dog dishes diary (list series) _leetcode 234. palindrome list

Make a list to determine whether the list is a palindrome.

Example 1:

输入: 1->2
输出: false

Example 2:

输入: 1->2->2->1
输出: true

The easiest way to most conventional, direct deposit to the value of the same linked list with the list, then that is a palindrome reverse a linked list.

# 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 = []
        while(head):
            res.append(head.val)
            head = head.next
        return res == res[::-1]

Recursion:

# 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:
        self.first = head #实例对象变量,在loop里可以调用
        def loop(cur):
            if cur:
                #走到None的时候,if cur false,直接跳到return True,回到None的上一个节点也就是最后一个节点。
                #只要有一个节点比较失败,则返回前一个cur时,loop(cur.next)为False,return False
                if not loop(cur.next): 
                    return False
                if self.first.val != cur.val:
                    return False
                self.first = self.first.next
            return True
        return loop(head)

Before thinking to cut the list in half Compare

# 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 = []
        while(head):
            res.append(head.val)
            head = head.next
        med = len(res)//2
        if res[:med]==res[med:][::-1] and len(res)%2==0:
            return True
        elif res[:med+1]==res[med:][::-1] and len(res)%2==1:
            return True
        else:
            return False

Pointer speed: thoughts on this cut in half with a similar speed pointer

# 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  #快慢指针指向头
        res = []  #用一个栈来装慢指针的值
        while(fast and fast.next):
            res.append(slow.val)
            slow = slow.next
            fast = fast.next.next
        # 这里慢指针返回中位数节点
        if fast:  #链表的长度是奇数
            slow = slow.next
        while(slow and res):
            val = res.pop()
            if val!=slow.val:
                return False
            slow = slow.next
        return True

After the stack may be advanced directly, using the characteristics of the stack

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

 

Published 44 original articles · won praise 0 · Views 1910

Guess you like

Origin blog.csdn.net/weixin_39331401/article/details/104546855