본문 바로가기
Programming/Data Structure

[Hackerrank] InsertNodeAtTail

by 읽고 쓰는 개발자 2020. 12. 2.

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

Insert a Node at the Tail of a Linked List | HackerRank

Create and insert a new node at the tail of a linked list.

www.hackerrank.com

package exam.complete;

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

public class InsertNodeAtTail {

    static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {
        SinglyLinkedListNode nowLinkedList = new SinglyLinkedListNode(data);
        if(head == null) head = nowLinkedList;
        else {
            SinglyLinkedListNode tail = head;
            while (tail.next != null) tail = tail.next;
            tail.next = nowLinkedList;
        }
        return head;

    }
}

'Programming > Data Structure' 카테고리의 다른 글

[Hackerrank] MergeTwoSortedLinkedLists  (0) 2020.12.02
[Hackerrank] MatchingStrings  (0) 2020.12.02
[Hackerrank] InsertNodeAtPosition  (0) 2020.12.02
[Hackerrank] InsertNodeAtHead  (0) 2020.12.02
[Hackerrank] GetNodeValue  (0) 2020.12.02