Day 15: Linked List

  • Instructions:
    • Complete the insert function such that it creates a new Node (data is passed as Node constructor argument) and inserts it at the tail end of the linked list referenced by the head parameter.
    • Once the new node is added, return the reference to the head node
    Excercise
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    class Node:
        def __init__(self,data):
            self.data = data
            self.next = None 
    class Solution: 
        def display(self,head):
            current = head
            while current:
                print(current.data,end=' ')
                current = current.next
    
        def insert(self,head,data): 
        #Complete this method
    
    mylist= Solution()
    T=int(input())
    head=None
    for i in range(T):
        data=int(input())
        head=mylist.insert(head,data)    
    mylist.display(head);  
    
    Solution
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    #!/bin/python3
    
    class Node:
        def __init__(self,data):
            self.data = data
            self.next = None 
    class Solution: 
        def display(self,head):
            current = head
            while current:
                print(current.data,end=' ')
                current = current.next
    
        def insert(self,head,data): 
        #Complete this method
            print(data, end= ' ')
    
    mylist= Solution()
    T=int(input())
    head=None
    for i in range(T):
        data=int(input())
        head=mylist.insert(head,data)    
    mylist.display(head);