Remove Nth Node From End of List

GAEEEGURI·2024년 12월 20일
0

leetcode

목록 보기
5/5

첫 번째 시도

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        # only 1 listnode
        if head.next == None:
            return None

        # when n equals to length of linked list
        tmp = head
        if self.listNodeLen(tmp) == n:
            return head.next
        
        # go to (n+1)th node from end of list
        while self.isNthFromEnd(tmp, n+1) == False:
            tmp = tmp.next

        # remove nth node from end of list
        if tmp.next.next == None: 
            tmp.next = None
        else:
            tmp.next = tmp.next.next

        return head
    
    def isNthFromEnd(self, head: Optional[ListNode], n: int) -> bool:
        tmp = head

        for _ in range(n):
            tmp = tmp.next if tmp else tmp
        
        if tmp == None:
            return True
        
        return False
    
    def listNodeLen(self, head: Optional[ListNode]) -> int:
        node_len = 0

        while head:
            node_len += 1
            head = head.next
        
        return node_len

        

22년도에 들었던 자료구조 수업이 새록새록 생각난다. 그때는 분명히 python을 훨씬 더 잘했던 것 같은데, 그때의 나로 돌아가기에는 시간이 좀 걸릴 것 같다.

이번부터 코드를 쓸 때 다른 사람이 읽을 코드라고 생각하고 쓰기로 했다.

  • 주석을 추가하고
  • method를 이용해 코드의 가독성을 높혔다.

GPT’s Solution

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        # Create a dummy node to handle edge cases (e.g., removing the head node)
        dummy = ListNode(0, head)
        first = dummy
        second = dummy

        # Move the first pointer n + 1 steps ahead
        for _ in range(n + 1):
            first = first.next

        # Move both pointers until the first pointer reaches the end
        while first:
            first = first.next
            second = second.next

        # Remove the nth node from the end
        second.next = second.next.next

        return dummy.next

이것도 two pointer 문제였다. 뒤에서 n번째 노드를 찾기 위해서 나는 method를 활용해 하나하나씩 확인하고 edge case별로 코드를 작성해야 했다. 반면 gpt는 dummy를 이용해 edge case를 다루었고, 두 개의 포인터를 이용해 뒤에서 n번째 노드를 바로 구했다.

마무리

  • 사람의 사고를 그대로 코드로 옮기는 건 그렇게 좋은게 아닌지도 모르겠다.
  • 항상 문제를 풀어내면 기분이 좋다가도, 가장 좋은 풀이를 보면 다시 시무룩해진다. 나아지고 있다는 데 의미를 둬야겠다.

0개의 댓글