알고리즘 문제/LEETCODE
[Python] Remove Nth Node from End of List
이융희
2021. 5. 6. 03:03
728x90
Medium 난이도 Acceptence 36.2% 문제
끝에서 n번째 노드를 제거하는 문제, 리스트로 값을 바꿔 원하는 노드를 지운 후 다시 노드로 바꿔 문제를 풀었다.
문제에서는 one pass로 풀 수 있냐고 한것에 더 맞는 정답을 위해선 list node class의 remove함수를 따로 구현해야 할것으로 보인다.
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
tempList = []
while head is not None:
tempList.append(head.val)
head = head.next
del tempList[len(tempList)-n]
dummy = ListNode()
output = dummy
for elem in tempList:
output.next = ListNode(elem)
output = output.next
return dummy.next