In: Computer Science
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
# Python program to check Linked List is sorted
# in descending order or not
''' Linked list Node '''
class Node:
def __init__(self, data):
self.data = data;
self.next = next;
# function to Check Linked List is
# sorted in descending order or not
def isSortedDesc(head):
if (head == None):
return True;
# Traverse the list till last Node and return
# False if a Node is smaller than or equal
# its next.
while(head.next != None):
t = head;
if (t.data <=
t.next.data):
return
False;
head = head.next
return True;
def newNode(data):
temp = Node(0);
temp.next = None;
temp.data = data;
return temp;
# Driver Code
if __name__ == '__main__':
head = newNode(7);
head.next = newNode(5);
head.next.next = newNode(4);
head.next.next.next = newNode(3);
if (isSortedDesc(head)):
print("Yes");
else:
print("No");