Problem: https://www.hackerrank.com/challenges/get-the-value-of-the-node-at-a-specific-position-from-the-tail/problem

Problem description:
You’re given the pointer to the head node of a linked list and a specific position. Counting backwards from the tail node of the linked list, get the value of the node at the given position. A position of 0 corresponds to the tail, 1 corresponds to the node before the tail and so on.

Code Sample: 1 using counter


def getNode(head, positionFromTail):
    ptr=head
    count=0
    while ptr is not None:
        count+=1
        ptr=ptr.next
    ptr=head 
    count=count-1  
    while ptr.next is not None:
        if count==positionFromTail:
            break
        ptr=ptr.next
        count-=1
    return ptr.data        
#!/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)

# Complete the getNode function below.

#
# For your reference:
#
# SinglyLinkedListNode:
#     int data
#     SinglyLinkedListNode next
#
#
def getNode(head, positionFromTail):
    ptr=head
    count=0
    while ptr is not None:
        count+=1
        ptr=ptr.next
    ptr=head 
    count=count-1  
    while ptr.next is not None:
        if count==positionFromTail:
            break
        ptr=ptr.next
        count-=1
    return ptr.data        

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    tests = int(input())

    for tests_itr in range(tests):
        llist_count = int(input())

        llist = SinglyLinkedList()

        for _ in range(llist_count):
            llist_item = int(input())
            llist.insert_node(llist_item)

        position = int(input())

        result = getNode(llist.head, position)

        fptr.write(str(result) + '\n')

    fptr.close()

Explanation: 1

Code Sample: 2 using pointer


def getNode(head, positionFromTail):
    ptr1=head
    ptr2=head
    count=0
    while ptr1.next is not None:
        ptr1=ptr1.next
        count+=1
        if count>positionFromTail:
            ptr2=ptr2.next
    return ptr2.data        
#!/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)

# Complete the getNode function below.

#
# For your reference:
#
# SinglyLinkedListNode:
#     int data
#     SinglyLinkedListNode next
#
#
def getNode(head, positionFromTail):
    ptr1=head
    ptr2=head
    count=0
    while ptr1.next is not None:
        ptr1=ptr1.next
        count+=1
        if count>positionFromTail:
            ptr2=ptr2.next
    return ptr2.data        
    

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    tests = int(input())

    for tests_itr in range(tests):
        llist_count = int(input())

        llist = SinglyLinkedList()

        for _ in range(llist_count):
            llist_item = int(input())
            llist.insert_node(llist_item)

        position = int(input())

        result = getNode(llist.head, position)

        fptr.write(str(result) + '\n')

    fptr.close()

Explanation: 2

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top