카테고리 없음
[LeetCode] Remove Nth Node From End of List
룰스
2019. 9. 21. 21:48
문제
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Approach
끝에서 부터 n번째이기 때문에 조금 신경을 써야한다.
vector<ListNode>를 만들고 node를 읽을 때 하나씩 벡터에 push_back해서 모든 노드를 저장한 다음,
전부 읽은 뒤에서 n+1번째랑 n-1를 이어주면 끝
n이 전체 list 길이일때만 조금 신경써주면 된다
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_RemoveNthNodeFromEndOfList.cpp
chi3236/algorithm
Contribute to chi3236/algorithm development by creating an account on GitHub.
github.com