본문 바로가기
Programming/Data Structure

[Hackerrank] DeleteNode

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

www.hackerrank.com/challenges/delete-a-node-from-a-linked-list/problem

 

Delete a Node | HackerRank

Delete a node from the linked list and return the head.

www.hackerrank.com

package exam.complete;
//https://www.hackerrank.com/challenges/delete-a-node-from-a-linked-list/problem

import exam.SinglyLinkedListNode;

public class DeleteNode {
    static SinglyLinkedListNode deleteNode(SinglyLinkedListNode head, int position) {
        if(position == 0) return head.next;
        SinglyLinkedListNode positionNode = head;
        for(int i = 0 ; i < position - 1 ; i++) positionNode = positionNode.next;
        positionNode.next = positionNode.next.next;
        return head;

    }
}

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

[Hackerrank] GetNodeValue  (0) 2020.12.02
[Hackerrank] FindMergePointofTwoLists  (0) 2020.12.02
[Hackerrank] CompareTwoLinkedLists  (0) 2020.12.02
[Hackerrank] BalancedBrackets  (0) 2020.12.02
[Hackerrank] reverseArray  (0) 2020.12.02