알고리즘/HackerRank
[해커랭크] Print in Reverse (C++)
라이언납시오
2021. 1. 15. 10:36
728x90
반응형
Print in Reverse | HackerRank
Print the elements of a linked list in reverse order, from tail to head
www.hackerrank.com
1. 주의할 점
- Stack에 대해서 알고있으면 된다
2. 구현
- 입력받은 SinglyLinkedListNode의 값을 Stack에 저장한다
- 모든 Node를 거쳤다면, Stack에서 1개씩 빼면서 출력한다
void reversePrint(SinglyLinkedListNode* head) {
stack<int> s;
while(head){
s.push(head->data);
head = head->next;
}
while(!s.empty()){
cout << s.top()<<'\n';
s.pop();
}
}
728x90
반응형