티스토리 뷰

반응형

Reverse Linked list

# 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: ListNode) -> ListNode:
        prev = None
        curr = head
        while curr:
            nex = curr.next
            curr.next = prev
            prev = curr
            curr = nex

        return prev

 

Linked List Palindrome

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

        return temp == temp[::-1]

 

반응형

'Development > Algorithms' 카테고리의 다른 글

Valid Palindrome in python3  (0) 2020.07.29
Leetocode 378 Kth Smallest Element in a Sorted Matrix  (2) 2020.06.22
Leetcode 35 Search insert Position  (2) 2020.06.08
Leetcode 67. Add Binary  (2) 2020.06.07
Leetcode 93. Restore IP address  (0) 2020.06.06
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함
반응형