Insert a Node at the tail of a linked list hackerrank solution C

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 def print_singly_linked_list[node, sep, fptr]: while node: fptr.write[str[node.data]] node = node.next if node: fptr.write[sep] def insertNodeAtTail[head, data]: cur = head if head == None: return SinglyLinkedListNode[data] while cur.next != None: cur = cur.next cur.next = SinglyLinkedListNode[data] return head if __name__ == '__main__': fptr = open[os.environ['OUTPUT_PATH'], 'w'] llist_count = int[input[]] llist = SinglyLinkedList[] for i in range[llist_count]: llist_item = int[input[]] llist_head = insertNodeAtTail[llist.head, llist_item] llist.head = llist_head print_singly_linked_list[llist.head, '\n', fptr] fptr.write['\n'] fptr.close[]




Video liên quan

Bài mới nhất

Chủ Đề