Insert a node after the n | Linked List node ; function to get a new node ; allocate memory for the node ; put in the data ; function to insert a node after the nth node from the end ; if list is empty ; get a new node for the value 'x   ; find length of the list , i . e , the number of nodes in the list ; traverse up to the nth node from the end ; insert the ' newNode ' by making the necessary adjustment in the links ; function to print the list ; Creating list 1 -> 3 -> 4 -> 5
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( data ) : NEW_LINE INDENT newNode = Node ( 0 ) NEW_LINE newNode . data = data NEW_LINE newNode . next = None NEW_LINE return newNode NEW_LINE DEDENT def insertAfterNthNode ( head , n , x ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT newNode = getNode ( x ) NEW_LINE ptr = head NEW_LINE len = 0 NEW_LINE i = 0 NEW_LINE while ( ptr != None ) : NEW_LINE INDENT len = len + 1 NEW_LINE ptr = ptr . next NEW_LINE DEDENT ptr = head NEW_LINE i = 1 NEW_LINE while ( i <= ( len - n ) ) : NEW_LINE INDENT ptr = ptr . next NEW_LINE i = i + 1 NEW_LINE DEDENT newNode . next = ptr . next NEW_LINE ptr . next = newNode NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT head = getNode ( 1 ) NEW_LINE head . next = getNode ( 3 ) NEW_LINE head . next . next = getNode ( 4 ) NEW_LINE head . next . next . next = getNode ( 5 ) NEW_LINE n = 4 NEW_LINE x = 2 NEW_LINE print ( " Original ▁ Linked ▁ List : ▁ " ) NEW_LINE printList ( head ) NEW_LINE insertAfterNthNode ( head , n , x ) NEW_LINE print ( ) NEW_LINE print ( " Linked ▁ List ▁ After ▁ Insertion : ▁ " ) NEW_LINE printList ( head ) NEW_LINE