Check if a Linked List is Pairwise Sorted | Linked List node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; The constructed linked list is : 10.15 . 9.9 .1 .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 start = None NEW_LINE def isPairWiseSorted ( head ) : NEW_LINE INDENT flag = True NEW_LINE temp = head NEW_LINE while ( temp != None and temp . next != None ) : NEW_LINE INDENT if ( temp . data > temp . next . data ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT temp = temp . next . next NEW_LINE DEDENT return flag NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT global start NEW_LINE new_node = Node ( 0 ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE start = head_ref NEW_LINE DEDENT start = None NEW_LINE push ( start , 5 ) NEW_LINE push ( start , 1 ) NEW_LINE push ( start , 9 ) NEW_LINE push ( start , 9 ) NEW_LINE push ( start , 15 ) NEW_LINE push ( start , 10 ) NEW_LINE if ( isPairWiseSorted ( start ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT