관리 메뉴

솜씨좋은장씨

[leetCode] 234. Palindrome Linked List (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 234. Palindrome Linked List (Python)

솜씨좋은장씨 2020. 9. 30. 18:19
728x90
반응형

Given a singly linked list, determine if it is a palindrome.

 

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Follow up:
Could you do it in O(n) time and O(1) space?

 

Solution

# 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:
        my_list = []
        
        while head != None:
            val = head.val
            
            my_list.append(val)
            
            head = head.next
            
        if my_list == my_list[::-1]:
            return True
        
        
        return False

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments