[LeetCode][Python3] 19. Remove Nth Node From End of List
2018. 11. 9. 02:47 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/remove-nth-node-from-end-of-list
My Solution :
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
first = second = dummy = ListNode(0)
dummy.next = head
for _ in range(n):
first = first.next
while first and first.next:
first = first.next
second = second.next
second.next = second.next.next
return dummy.next
Comment :
Cracking the Coding Interview 책에서 봤던 문제라 별 고민 없이 two pointer 전략으로 접근하였다. Linked List는 항상 Edge Case 때문에 고생하는데 dummy head를 활용하는게 일종의 트릭이 될 수 있다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 46. Permutations (0) | 2018.11.12 |
---|---|
[LeetCode][Python3] 36. Valid Sudoku (0) | 2018.11.11 |
[LeetCode][Python3] 34. Find First and Last Position of Element in Sorted Array (0) | 2018.11.11 |
[LeetCode][Python3] 22. Generate Parentheses (0) | 2018.11.10 |
[LeetCode][Python3] 17. Letter Combinations of a Phone Number (0) | 2018.11.09 |
[LeetCode][Python3] 11. Container With Most Water (0) | 2018.11.07 |
[LeetCode][Python3] 108. Convert Sorted Array to Binary Search Tree (0) | 2018.11.06 |
[LeetCode][Python3] 387. First Unique Character in a String (0) | 2018.11.05 |
최근에 달린 댓글 최근에 달린 댓글