Find a peak element in Linked List
Last Updated :
23 Jan, 2023
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
int findPeak( struct Node* head)
{
if (head == NULL)
return -1;
if (head->next == NULL)
return head->data;
int prev = head->data;
Node *curr;
for (curr = head->next; curr->next != NULL;
curr = curr->next) {
if (curr->data > curr->next->data
&& curr->data > prev)
return curr->data;
prev = curr->data;
}
if (curr->data > prev)
return curr->data;
else
return -1;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 4);
push(&head, 8);
push(&head, 6);
push(&head, 1);
cout << "Peak element is: "
<< findPeak(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static int findPeak( Node head)
{
if (head == null )
return - 1 ;
if (head.next == null )
return head.data;
int prev = head.data;
Node curr;
for (curr = head.next; curr.next != null ;
curr = curr.next)
{
if (curr.data > curr.next.data
&& curr.data > prev)
return curr.data;
prev = curr.data;
}
if (curr.data > prev)
return curr.data;
else
return - 1 ;
}
public static void main(String args[])
{
Node head = null ;
head=push(head, 12 );
head=push(head, 4 );
head=push(head, 8 );
head=push(head, 6 );
head=push(head, 1 );
System.out.print( "Peak element is: "
+ findPeak(head));
}
}
|
Python3
class Node :
def __init__( self ):
self .data = 0
self . next = None
def push( head_ref, new_data) :
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def findPeak( head):
if (head = = None ) :
return - 1
if (head. next = = None ) :
return head.data
prev = head.data
curr = head. next
while ( curr. next ! = None ):
if (curr.data > curr. next .data and curr.data > prev) :
return curr.data
prev = curr.data
curr = curr. next
if (curr.data > prev) :
return curr.data
else :
return - 1
head = None
head = push(head, 12 )
head = push(head, 4 )
head = push(head, 8 )
head = push(head, 6 )
head = push(head, 1 )
print ( "Peak element is: " , findPeak(head))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static int findPeak(Node head)
{
if (head == null )
return -1;
if (head.next == null )
return head.data;
int prev = head.data;
Node curr;
for (curr = head.next; curr.next != null ;
curr = curr.next)
{
if (curr.data > curr.next.data
&& curr.data > prev)
return curr.data;
prev = curr.data;
}
if (curr.data > prev)
return curr.data;
else
return -1;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 1);
Console.Write( "Peak element is: " +
findPeak(head));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function findPeak(head) {
if (head == null ) return -1;
if (head.next == null ) return head.data;
var prev = head.data;
var curr;
for (curr = head.next; curr.next != null ; curr = curr.next)
{
if (curr.data > curr.next.data && curr.data > prev)
return curr.data;
prev = curr.data;
}
if (curr.data > prev) return curr.data;
else return -1;
}
var head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 1);
document.write( "Peak element is: " + findPeak(head));
</script>
|
Output
Peak element is: 8
Time Complexity: O(N) where N is the number of elements in the linked list.
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...