일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 715. Range Module
- Class
- Decorator
- 43. Multiply Strings
- Regular Expression
- t1
- Substring with Concatenation of All Words
- iterator
- Generator
- attribute
- Protocol
- LeetCode
- 시바견
- 운영체제
- data science
- shiba
- Python
- 프로그래머스
- Python Implementation
- kaggle
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- 109. Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- 밴픽
- DWG
- concurrency
- Convert Sorted List to Binary Search Tree
- 파이썬
- Python Code
Archives
- Today
- Total
Scribbling
LeetCode: 19. Remove Nth Node From End of List 본문
Computer Science/Coding Test
LeetCode: 19. Remove Nth Node From End of List
focalpoint 2021. 8. 22. 22:44# 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]:
length = 0
cur = head
while cur != None:
length += 1
cur = cur.next
prv, cur = None, head
num_move = length - n
while num_move > 0:
prv = cur
cur = cur.next
num_move -= 1
if cur == head:
return head.next
prv.next = cur.next
return head
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 25. Reverse Nodes in k-Group (0) | 2021.08.25 |
---|---|
LeetCode: 22. Generate Parentheses (0) | 2021.08.24 |
LeetCode: 18. 4Sum (0) | 2021.08.22 |
LeetCode: 17. Letter Combinations of a Phone Number (0) | 2021.08.21 |
LeetCode: 16. 3Sum Closest (0) | 2021.08.21 |