https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list/problem

연결리스트의 마지막에 노드를 삽입하는 문제다.
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
    if (head == NULL) {
        return create_singly_linked_list_node(data);
    }
    
    SinglyLinkedListNode *node = head;
    while (node->next != NULL) {
        node = node->next;
    }
    
    node->next = create_singly_linked_list_node(data);
    return head;
}head가 NULL이면 노드를 삽입한다. node에 head를 복사해 head가 NULL이 아니면 다음 노드가 빈 노드일 때까지 이동해 새로운 노드를 삽입한다. head를 반환한다.

'HackerRank > Data Structures' 카테고리의 다른 글
| [HackerRank] Trees > Tree : Height of a Binary Tree (0) | 2021.10.10 | 
|---|---|
| [HackerRank] Stacks > Maximum Element (0) | 2021.10.03 | 
| [HackerRank] Linked Lists > Print the Elements of a Linked List (0) | 2021.08.14 | 
| [HackerRank C] Tree : Inorder Traversal (0) | 2021.06.27 | 
| [HackerRank C] Tree : Postorder Traversal (0) | 2021.06.27 |