Bitonic point in the given linked list

Given a linked list with distinct elements, the task is to find the bitonic point in the given linked list. If there is no such point then print -1.

Examples:

Input: 1 -> 2 -> 3 -> 4 -> 3 -> 2 -> 1 -> NULL
Output: 4
1 -> 2 -> 3 -> 4 is strictly increasing.
4 -> 3 -> 2 -> 1 -> NULL is strictly decreasing.

Input: 97 -> 98 -> 99 -> 91 -> NULL
Output: 99

Approach: A Bitonic Point is a point in bitonic sequence before which elements are strictly increasing and after which elements are strictly decreasing. A Bitonic point doesn’t exist if array is only decreasing or only increasing. So, find the first node such that the value of the node next to it is strictly smaller. Start traversing the linked list from that node onwards and if every other node is strictly smaller than its previous node then the found node was out bitonic sequence else the given linked list doesn’t contain a valid bitonic sequence. Note that an empty list or a list with a single node doesn’t represent a valid bitonic sequence.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Node for linked list
class Node {
public:
    int data;
    Node* next;
};
  
// Function to insert a node at
// the head of the linked list
Node* push(Node** head_ref, int data)
{
    Node* new_node = new Node;
    new_node->data = data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
  
// Function to return the bitonic
// of the given linked list
int bitonic_point(Node* node)
{
    // If list is empty
    if (node == NULL)
        return -1;
  
    // If list contains only
    // a single node
    if (node->next == NULL)
        return -1;
  
    // Invalid bitonic sequence
    if (node->data > node->next->data)
        return -1;
  
    while (node->next != NULL) {
  
        // If current node is the bitonic point
        if (node->data > node->next->data)
            break;
  
        // Get to the next node in the list
        node = node->next;
    }
  
    int bitonicPoint = node->data;
    // Nodes must be in descending
    // starting from here
    while (node->next != NULL) {
  
        // Out of order node
        if (node->data < node->next->data)
            return -1;
  
        // Get to the next node in the list
        node = node->next;
    }
  
    return bitonicPoint;
}
  
// Driver code
int main()
{
    Node* head = NULL;
  
    push(&head, 100);
    push(&head, 201);
    push(&head, 399);
    push(&head, 490);
    push(&head, 377);
    push(&head, 291);
    push(&head, 100);
  
    cout << bitonic_point(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG
{
      
// Node for linked list 
static class Node 
    int data; 
    Node next; 
}; 
  
// Function to insert a node at 
// the head of the linked list 
static Node push(Node head_ref, int data) 
    Node new_node = new Node(); 
    new_node.data = data; 
    new_node.next = (head_ref); 
    (head_ref) = new_node;
    return head_ref;
  
// Function to return the bitonic 
// of the given linked list 
static int bitonic_point(Node node) 
    // If list is empty 
    if (node == null
        return -1
  
    // If list contains only 
    // a single node 
    if (node.next == null
        return -1
  
    // Invalid bitonic sequence 
    if (node.data > node.next.data) 
        return -1
  
    while (node.next != null
    
  
        // If current node is the bitonic point 
        if (node.data > node.next.data) 
            break
  
        // Get to the next node in the list 
        node = node.next; 
    
  
    int bitonicPoint = node.data; 
      
    // Nodes must be in descending 
    // starting from here 
    while (node.next != null
    
  
        // Out of order node 
        if (node.data < node.next.data) 
            return -1
  
        // Get to the next node in the list 
        node = node.next; 
    
  
    return bitonicPoint; 
  
// Driver code 
public static void main(String args[])
    Node head = null
  
    head=push(head, 100); 
    head=push(head, 201); 
    head=push(head, 399); 
    head=push(head, 490); 
    head=push(head, 377); 
    head=push(head, 291); 
    head=push(head, 100); 
  
    System.out.println(bitonic_point(head)); 
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
      
class GFG
{
      
// Node for linked list 
public class Node 
    public int data; 
    public Node next; 
}; 
  
// Function to insert a node at 
// the head of the linked list 
static Node push(Node head_ref, int data) 
    Node new_node = new Node(); 
    new_node.data = data; 
    new_node.next = (head_ref); 
    (head_ref) = new_node;
    return head_ref;
  
// Function to return the bitonic 
// of the given linked list 
static int bitonic_point(Node node) 
    // If list is empty 
    if (node == null
        return -1; 
  
    // If list contains only 
    // a single node 
    if (node.next == null
        return -1; 
  
    // Invalid bitonic sequence 
    if (node.data > node.next.data) 
        return -1; 
  
    while (node.next != null
    
  
        // If current node is the bitonic point 
        if (node.data > node.next.data) 
            break
  
        // Get to the next node in the list 
        node = node.next; 
    
  
    int bitonicPoint = node.data; 
      
    // Nodes must be in descending 
    // starting from here 
    while (node.next != null
    
  
        // Out of order node 
        if (node.data < node.next.data) 
            return -1; 
  
        // Get to the next node in the list 
        node = node.next; 
    
  
    return bitonicPoint; 
  
// Driver code 
public static void Main(String []args)
    Node head = null
  
    head=push(head, 100); 
    head=push(head, 201); 
    head=push(head, 399); 
    head=push(head, 490); 
    head=push(head, 377); 
    head=push(head, 291); 
    head=push(head, 100); 
  
    Console.WriteLine(bitonic_point(head)); 
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

490


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : andrew1234, 29AjayKumar