Delete a node at a specific position in a linked list hackerrank solution

Problem solution in Python programming.

#!/bin/python3 import math import os import random import re import sys class SinglyLinkedListNode: def __init__(self, node_data): self.data = node_data self.next = None class SinglyLinkedList: def __init__(self): self.head = None self.tail = None def insert_node(self, node_data): node = SinglyLinkedListNode(node_data) if not self.head: self.head = node else: self.tail.next = node self.tail = node def print_singly_linked_list(node, sep, fptr): while node: fptr.write(str(node.data)) node = node.next if node: fptr.write(sep) def deleteNode(head, position): if position == 0: return head.next current = head for i in range(position-1): current = current.next current.next = current.next.next return head if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') llist_count = int(input()) llist = SinglyLinkedList() for _ in range(llist_count): llist_item = int(input()) llist.insert_node(llist_item) position = int(input()) llist1 = deleteNode(llist.head, position) print_singly_linked_list(llist1, ' ', fptr) fptr.write('\n') fptr.close()




HACKERRANK SOLUTION: Delete a Node

//COPY PASTE THIS PART OF CODE IN THE GIVEN BLANK SPACE OF YOUR EDITOR….

static SinglyLinkedListNode deleteNode(SinglyLinkedListNode head, int position) {

if(position == 0)

{

head = head.next;

return head;

}

else{

Python Solution For HackerRank Problem: Delete a Node

12/13/2020bytebot
#hackerrank-solutions#codingchallenge#python#data-structures#linked-list

Delete a Node, is a HackerRank problem from Linked Lists subdomain. In this post we will see how we can solve this challenge in Python