Given a Linked list of integers. The task is to find a peak element in it. An element in the list is said to be peak if it is NOT smaller than its neighbors. For corner elements, we need to consider only one neighbor. For example:
- If the input list is {5 -> 10 -> 20 -> 15}, 20 is the only peak element.
- For input list {10 -> 20 -> 15 -> 2 -> 23 -> 90 -> 67}, there are two peak elements: 20 and 90. Note that it is needed to return any one peak element.
Following corner cases give a better idea about the problem:
- If the input list is sorted in strictly increasing order, the last element is always a peak element. For example, 50 is peak element in {10 -> 20 -> 30 -> 40 -> 50}.
- If the input list is sorted in strictly decreasing order, the first element is always a peak element. 100 is the peak element in {100 -> 80 -> 60 -> 50 -> 20}.
- If all elements of the input list are same, every element is a peak element.
Examples:
Input : List = {1 -> 6 -> 8 -> 4 -> 12}
Output : 8
Input : List = {10 -> 20 -> 15 -> 2 -> 23 -> 90 -> 67}
Output : 90
The idea is to traverse the linked list and check if the current element is a peak element or not. If yes then return the current element else move forward in the list.
The current element will be a peak element if it is greater than its previous and next elements.
Below program illustrate the above approach:
// C++ implementation to find the peak // element in the Linked List #include <bits/stdc++.h> using namespace std; /* A Linked list node */struct Node { int data; struct Node* next; }; // function to insert a node at the // beginning of the linked list void push(struct Node** head_ref, int new_data) { struct Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } // Function to find the peak element int findPeak(struct Node* head) { // Return -1 to indicate that // peak does not exist if (head == NULL) return -1; // If there is only one node if (head->next == NULL) return head->data; // Traverse till last node (starting from // second node) int prev = head->data; Node *curr; for (curr = head->next; curr->next != NULL; curr = curr->next) { // check if current node is greater // than both neighbours if (curr->data > curr->next->data && curr->data > prev) return curr->data; prev = curr->data; } // We reach here when curr is last node if (curr->data > prev) return curr->data; // Peak does not exists else return -1; } // Driver program int main() { struct Node* head = NULL; // create linked list 1->6->8->4->12 push(&head;, 12); push(&head;, 4); push(&head;, 8); push(&head;, 6); push(&head;, 1); cout << "Peak element is: " << findPeak(head); return 0; } |
Peak element is: 8
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
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.



