Programming/Data Structure
[Hackerrank] InsertNodeAtTail
읽고 쓰는 개발자
2020. 12. 2. 22:32
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;
}
}