A strictly increasing linked list in Python

A strictly increasing linked list is one where each node's value is greater than the previous node's value. We need to traverse the list and check if the values are in strictly ascending order.

So, if the input is like [2, 61, 105, 157], then the output will be True because each element is greater than the previous one.

Algorithm

To solve this, we will follow these steps −

  • Define a function solve() that takes the head of the linked list

  • If head.next is null, return True (single node is considered strictly increasing)

  • If head.val >= head.next.val, return False (not strictly increasing)

  • Recursively call solve(head.next) to check the rest of the list

Implementation

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

class Solution:
    def solve(self, head):
        if head.next is None:
            return True
        if head.val >= head.next.val:
            return False
        return self.solve(head.next)

# Test the solution
ob = Solution()
head = make_list([2, 61, 105, 157])
print(ob.solve(head))
True

Testing with Non-Strictly Increasing List

Let's test with a list that is not strictly increasing ?

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

class Solution:
    def solve(self, head):
        if head.next is None:
            return True
        if head.val >= head.next.val:
            return False
        return self.solve(head.next)

# Test with non-strictly increasing list
ob = Solution()
head = make_list([2, 61, 50, 157])  # 61 > 50, so not strictly increasing
print(ob.solve(head))
False

Iterative Approach

We can also solve this problem using an iterative approach instead of recursion ?

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

class Solution:
    def solve_iterative(self, head):
        current = head
        while current and current.next:
            if current.val >= current.next.val:
                return False
            current = current.next
        return True

# Test iterative approach
ob = Solution()
head = make_list([1, 3, 5, 7, 9])
print(ob.solve_iterative(head))
True

Comparison

Approach Time Complexity Space Complexity Best For
Recursive O(n) O(n) Clean, readable code
Iterative O(n) O(1) Memory efficiency

Conclusion

Both recursive and iterative approaches efficiently check if a linked list is strictly increasing. The recursive approach is more intuitive, while the iterative approach is more memory-efficient with O(1) space complexity.

Updated on: 2026-03-25T10:12:40+05:30

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements