# Longest increasing sublist in a linked list

Given a singly linked list and we want to count the elements that are continuously increasing and print the increasing linked list.

Examples:

Input : 8 -> 5 -> 7 -> 10 -> 9 -> 11 -> 12 -> 13 -> NULL Output : Number of continuously increasing elements = 4 Increasing linked list : 9 11 12 13 Input : 5 -> 12 -> 18 -> 7 -> 12 -> 15 -> NULL Output : Number of continuously increasing elements = 3 Increasing linked list = 5 12 18

The idea is to traverse singly linked list and compare curr->data with curr->next->data where curr is current node being traversed. If curr->data is smaller then curr->next->data then curr pointer point to curr->next and increment the length (continuous increasing element) by one. If the condition is false then compare the length with max and if max is less than len then assign the len value to max. Continue this process until head not equal to NULL. Also find the starting index of continuous increasing element. Next traverse the linked list and display the continuous increasing element in linked list.

`// Program to count maximum number of continuous ` `// increasing element in linked list and display ` `// the elements of linked list. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// Function that count maximum number of continuous ` `// increasing elements in linked list and display ` `// the list. ` `void` `countIncreasingElements(` `struct` `Node *head) ` `{ ` ` ` `// Traverse the list and keep track of max increasing ` ` ` `// and current increasing lengths ` ` ` `int` `curr_len = 1, max_len = 1; ` ` ` `int` `total_count = 1, res_index = 0; ` ` ` `for` `(Node *curr=head; curr->next!=NULL; curr=curr->next) ` ` ` `{ ` ` ` `// Compare head->data with head->next->data ` ` ` `if` `(curr->data < curr->next->data) ` ` ` `curr_len++; ` ` ` `else` ` ` `{ ` ` ` `// compare maximum length with len. ` ` ` `if` `(max_len < curr_len) ` ` ` `{ ` ` ` `max_len = curr_len; ` ` ` `res_index = total_count - curr_len; ` ` ` `} ` ` ` ` ` `curr_len = 1; ` ` ` `} ` ` ` `total_count++; ` ` ` `} ` ` ` ` ` `if` `(max_len < curr_len) ` ` ` `{ ` ` ` `max_len = curr_len; ` ` ` `res_index = total_count - max_len; ` ` ` `} ` ` ` ` ` `// Print the maximum number of continuous elements ` ` ` `// in linked list. ` ` ` `cout << ` `"Number of continuously increasing element"` ` ` `" in list : "` `; ` ` ` `cout << max_len << endl; ` ` ` ` ` `// Traverse the list again to print longest increasing ` ` ` `// sublist ` ` ` `int` `i = 0; ` ` ` `cout << ` `"Increasing linked list"` `<< endl; ` ` ` `for` `(Node* curr=head; curr!=NULL; curr=curr->next) ` ` ` `{ ` ` ` `// compare with starting index and index of ` ` ` `// maximum increasing elements if both are ` ` ` `// equals then execute it. ` ` ` `if` `(i == res_index) ` ` ` `{ ` ` ` `// loop until max greater then 0. ` ` ` `while` `(max_len > 0) ` ` ` `{ ` ` ` `// Display the list and temp point ` ` ` `// to the next element. ` ` ` `cout << curr->data << ` `" "` `; ` ` ` `curr = curr->next; ` ` ` `max_len--; ` ` ` `} ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Function to insert an element at the beginning ` `void` `push(` `struct` `Node** head, ` `int` `data) ` `{ ` ` ` `struct` `Node* newNode = ` `new` `Node; ` ` ` `newNode->data = data; ` ` ` `newNode->next = (*head); ` ` ` `(*head) = newNode; ` `} ` ` ` `// Display linked list. ` `void` `printList(` `struct` `Node* node) ` `{ ` ` ` `while` `(node != NULL) { ` ` ` `cout << node->data << ` `" "` `; ` ` ` `node = node->next; ` ` ` `} ` ` ` `cout << endl; ` `} ` ` ` `// Drier functions ` `int` `main() ` `{ ` ` ` `// Create a node and initialize with NULL ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// push() insert node in linked list. ` ` ` `// 15 -> 18 -> 5 -> 8 -> 11 -> 12 ` ` ` `push(&head, 12); ` ` ` `push(&head, 11); ` ` ` `push(&head, 8); ` ` ` `push(&head, 5); ` ` ` `push(&head, 18); ` ` ` `push(&head, 15); ` ` ` `cout << ` `"Linked list:"` `<< endl; ` ` ` `printList(head); ` ` ` ` ` `// Function call countIncreasingElements(head) ` ` ` `// cout << countIncreasingElements(head) << endl; ` ` ` `countIncreasingElements(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Linked list: 15 18 5 8 11 12 Number of continuously increasing element in list :4 Increasing linked list 5 8 11 12

This article is contributed by **Dharmendra kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way