
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Strictly Increasing Linked List in Python
Suppose we have head of a singly linked list, we have to check whether the values of the nodes are sorted in a strictly ascending order or not.
So, if the input is like [2,61,105,157], then the output will be True.
To solve this, we will follow these steps −
Define a function solve() . This will take head
-
if head.next is null, then
return True
-
if head.val >= head.next.val, then
return False
return solve(head.next)
Let us see the following implementation to get better understanding −
Example
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 == None: return True if head.val >= head.next.val: return False return self.solve(head.next) ob = Solution() head = make_list([2,61,105,157]) print(ob.solve(head))
Input
[2,61,105,157]
Output
True
Advertisements