Given the head of a singly linked list, return true if it is a palindrome or false otherwise. Example 1:Input: head = [1,2,2,1]Output: trueExample 2:Input: head = [1,2]Output: false
Question
Given the head of a singly linked list, return true if it is a palindrome or false otherwise. Example 1:Input: head = [1,2,2,1]Output: trueExample 2:Input: head = [1,2]Output: false
Solution
Para determinar si una lista enlazada es un palíndromo, podemos seguir estos pasos:
-
Encontrar la mitad de la lista: Usaremos dos punteros, uno que avanza una posición a la vez (lento) y otro que avanza dos posiciones a la vez (rápido). Cuando el puntero rápido llegue al final, el puntero lento estará en la mitad de la lista.
-
Invertir la segunda mitad de la lista: A partir del puntero lento, invertimos la segunda mitad de la lista.
-
Comparar las dos mitades: Comparamos la primera mitad de la lista con la segunda mitad invertida. Si todas las posiciones coinciden, la lista es un palíndromo.
-
Restaurar la lista (opcional): Si es necesario, podemos restaurar la lista a su estado original invirtiendo nuevamente la segunda mitad.
Aquí está el código que implementa estos pasos:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def isPalindrome(head: ListNode) -> bool:
if not head or not head.next:
return True
# Encontrar la mitad de la lista
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# Invertir la segunda mitad de la lista
Similar Questions
You are given a singly linked list, determine if it is a palindrome.A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).Write a function that takes the head of a linked list as input and returns True if the linked list is a palindrome, and False otherwise.Sample input:4 //size1 2 2 1 //listOutput:TrueExplanation:The linked list reads the same forward and backward, making it a palindrome.
Given a linked list with string data, check whether the combined string formed is palindrome. If the combined string is palindrome then return true otherwise return false.Example:Input:Output : trueExplanation: As string "abcddcba" is palindrome the function should return true.Input:Output : falseExplanation: As string "abcdba" is not palindrome the function should return false.Expected Time Complexity: O(n)Expected Auxiliary Space: O(n)Constraints:1 <= Node.data.length<= 1031<=list.length<=103
Given an integer x, return true if x is a palindrome, and false otherwise. Example 1:Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.Example 2:Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Example 3:Input: x = 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.
Given an integer x, return true if x is a palindrome, and false otherwise.
Write a python program to check the given string is palindrome or not.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.