본문 바로가기
Programming/Data Structure

[Hackerrank] InsertNodeAtPosition

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

www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem

 

Insert a node at a specific position in a linked list | HackerRank

Insert a node at a specific position in a linked list.

www.hackerrank.com

package exam.complete;
//https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list/problem

import exam.SinglyLinkedListNode;

public class InsertNodeAtPosition {
    static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
        SinglyLinkedListNode returnHead = head;
        SinglyLinkedListNode node = new SinglyLinkedListNode(data);
        for (int i = 0; i < position - 1; i++) head = head.next;
        node.next = head.next;
        head.next = node;
        return returnHead;
    }
}

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

[Hackerrank] MatchingStrings  (0) 2020.12.02
[Hackerrank] InsertNodeAtTail  (0) 2020.12.02
[Hackerrank] InsertNodeAtHead  (0) 2020.12.02
[Hackerrank] GetNodeValue  (0) 2020.12.02
[Hackerrank] FindMergePointofTwoLists  (0) 2020.12.02