VOOZH about

URL: https://www.geeksforgeeks.org/dsa/longest-increasing-subsequence-in-given-linked-list/

⇱ Longest Increasing Subsequence in given Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest Increasing Subsequence in given Linked List

Last Updated : 23 Jul, 2025

Given a sequence of numbers in the form of a linked list lis. Find the length of the Longest Increasing Subsequence(LIS) of the given Linked List.

Examples:

Input:  list = 3->10->2->1->20
Output: 3
Explanation: The longest increasing subsequence is 3->10-> 20

Input:  list = 3-> 2
Output: 1
Explanation: The longest increasing subsequence are 3 and 2

Input: list = 50->3->10->7->40->80
Output: Length of LIS = 4
Explanation: The longest increasing subsequence is {3->7->40->80} or {3->10->40->80}

Approach: The basic intuition of the solution is to start iterating from the first node to the end of linked list .In the process of moving calculate length of LIS ending at every node and store it in a count variable. Finally, calculate maximum count value among all nodes. Follow the steps mentioned below to solve the problem: 

  • Traverse the linked list from the starting node.
    • LIS length of  a  linked list with one node is 1 .So we initialize  every node count variable to 1.
    • For every ith node traverse the first (i-1) nodes and do the following:
      • if value of current node is greater than the value of previous node, extend  sequence length.
      • As maximum length ending with current  node is required. select node from first (i-1) nodes which satisfy the previous condition and have maximum count value .
  • Once all the nodes are traversed following the above procedure .find maximum count value among all nodes.
  • The maximum count value is the required length of the LIS.

Below is the implementation of the above approach:

 
 


Output
3


 

Time Complexity: O(N * N) where N is the length of the linked list
Auxiliary Space: O(N)


 

Comment
Article Tags: