
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if linked list is sorted (Iterative and Recursive) in Python
Suppose we have a linked list we have to define two functions to check whether the linked list is sorted in non-increasing order or not. One of the method will work as iterative manner and another one in recursive manner.
So, if the input is like L = [15, 13, 8, 6, 4, 2], then the output will be True.
To solve this, we will follow these steps −
- Define a function solve_iter(). This will take head
- if head is null, then
- return True
- while next of head is not null, do
- current := head
- if value of current <= value of (next of current), then
- return False
- head := next of head
- return True
- Define a function solve_rec() . This will take head
- if head is null or next of head is null, then
- return True
- return true when (val of head > value of (next of head) is not 0 and solve_rec(next of head) is true) otherwise false
Example
Let us see the following implementation to get better understanding −
class ListNode: def __init__(self, data, next = None): self.val = data self.next = next def make_list(elements): head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head def solve_iter(head): if head == None: return True while head.next != None: current = head if current.val <= current.next.val: return False head = head.next return True def solve_rec(head): if head == None or head.next == None: return True return head.val > head.next.val and solve_rec(head.next) L = make_list([15, 13, 8, 6, 4, 2]) print(solve_iter(L)) print(solve_rec(L))
Input
[15, 13, 8, 6, 4, 2]
Output
True True
- Related Articles
- Program to check if an array is sorted or not (Iterative and Recursive) in C
- Find Length of a Linked List (Iterative and Recursive) in C++
- Check if a Linked List is Pairwise Sorted in C++
- Check if list is sorted or not in Python
- Check if a linked list is Circular Linked List in C++
- Recursive insertion and traversal linked list in C++
- Check if an array is sorted and rotated in Python
- Binary Search (Recursive and Iterative) in C Program
- Check if absolute difference of consecutive nodes is 1 in Linked List in Python
- Check if elements of Linked List are present in pair in Python
- C Program for Binary Search (Recursive and Iterative)?
- Count rotations in sorted and rotated linked list in C++
- Construct a linked list from 2D matrix (Iterative Approach) in C++
- Print the alternate nodes of linked list (Iterative Method) in C language
- Count consonants in a string (Iterative and recursive methods) in C++

Advertisements