Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 편스토랑 우승상품
- 프로그래머스
- Docker
- 프로그래머스 파이썬
- Real or Not? NLP with Disaster Tweets
- 캐치카페
- Kaggle
- 편스토랑
- ubuntu
- programmers
- ChatGPT
- 우분투
- 더현대서울 맛집
- Git
- gs25
- 금융문자분석경진대회
- hackerrank
- dacon
- 코로나19
- 파이썬
- 데이콘
- github
- Baekjoon
- 자연어처리
- 백준
- PYTHON
- AI 경진대회
- SW Expert Academy
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 19. Remove Nth Node From End of List (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 19. Remove Nth Node From End of List (Python)
솜씨좋은장씨 2020. 11. 16. 00:05728x90
반응형
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Follow up: Could you do this in one pass?
Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
Constraints:
- The number of nodes in the list is sz.
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
Solution
# 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: ListNode, n: int) -> ListNode:
answerList = ListNode()
answer = None
linked_list = []
cnt = 0
while True:
if head == None:
break
value = head.val
linked_list.append(value)
head = head.next
linked_list_len = len(linked_list)
check_num = linked_list_len - n
check_flag = False
for i in range(len(linked_list)):
if i == check_num:
continue
elif check_flag == False:
answerList = ListNode(linked_list[i])
check_flag = True
else:
new_node = ListNode(linked_list[i])
currNode = answerList
while currNode.next != None:
currNode = currNode.next
currNode.next = new_node
if check_flag == False:
return answerList.next
return answerList
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 77. Combinations (Python) (0) | 2020.11.18 |
---|---|
[leetCode] 47. Permutations II (Python) (0) | 2020.11.17 |
[leetCode] 397. Integer Replacement (Python) (0) | 2020.11.15 |
[leetCode] 747. Largest Number At Least Twice of Others (Python) (0) | 2020.11.13 |
[leetCode] 859. Buddy Strings (Python) (0) | 2020.11.12 |
Comments