Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 | 31 |
Tags
- 알고리즘풀이
- LeetCode
- python xor
- 파이썬알고리즘
- leetcode풀이
- 잇츠디모
- 릿코드 풀기
- 릿코드풀이
- leetcode풀기
- python priority queue
- 릿코드풀기
- python zip_longest
- 파이썬 프로그래머스
- python Leetcode
- 릿코드 파이썬
- binary search
- leetcode 풀기
- python sorted
- python 릿코드
- 코틀린기초
- 파이썬 알고리즘
- 상가수익률계산기
- python 알고리즘
- 파이썬 릿코드
- 알고리즘풀기
- 릿코드
- 파이썬릿코드풀기
- 파이썬릿코드
- 파이썬 알고리즘 풀기
- 파이썬알고리즘풀기
Archives
- Today
- Total
소프트웨어에 대한 모든 것
LeetCode 풀기 - 19. Remove Nth Node From End of List 본문
반응형
19. Remove Nth Node From End of List
Remove Nth Node From End of List - LeetCode Discuss
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
문제)

솔루션1)
스택 자료구조를 사용한다.
모든 노드를 순회하면서 스택에 노드를 추가하고 n번 만큼 pop()수행해서 삭제 대상 노드를 찾는다.
# 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]:
node = head
stack = deque()
while node:
stack.append(node)
node = node.next
for i in range(n-1):
stack.pop()
target_node = stack[-1]
if len(stack) == 1:
head = target_node.next
else:
prev_target_node = stack[-2]
prev_target_node.next = target_node.next
return head
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
| LeetCode 풀기 - 567. Permutation in String (0) | 2021.11.16 |
|---|---|
| LeetCode 풀기 - 3. Longest Substring Without Repeating Characters (0) | 2021.11.16 |
| LeetCode 풀기 - 876. Middle of the Linked List (0) | 2021.11.15 |
| LeetCode 풀기 - 739. Daily Temperatures (0) | 2021.11.15 |
| LeetCode 풀기 - 1413. Minimum Value to Get Positive Step by Step Sum (0) | 2021.11.12 |
Comments