Algorithm/Leetcode
[LeetCode] 234. Palindrome Linked List
에버
2021. 1. 23. 19:56
문제 링크 : https://leetcode.com/problems/palindrome-linked-list/
Palindrome Linked List - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
이 문제는 linked list가 주어졌을 때, 해당 list가 팰린드롬인지 확인하는 문제이다.
풀이방법
해당 list가 팰린드롬인지 아닌지 T/F를 판단하는 문제이기 때문에 linked list의 value를 새로운 list를 만들어서 저장 후 팰린드롬인지 확인하였다.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
val_list = []
curr = head
while curr:
val_list.append(curr.val)
curr = curr.next
return val_list == val_list[::-1]