Find a peak element in Linked List
Last Updated :
14 Aug, 2024
Given a linked list of integers, the task is to find a peak element in the given linked list. An element is a peak, if it is greater than or equals to its neighbors. For boundary elements, consider only one neighbor.
Examples:
Input : List = {1 -> 6 -> 8 -> 4 -> 12}
Output : 8
Explanation: In this list, 8 is greater than both its neighbors (6 and 4), making it a peak element. Other elements do not satisfy the peak condition.
Input : List = {1->2->3->4}
Output : 4
Explanation: The list is strictly increasing. In such cases, the last element (4) is always a peak, as it is greater than its only neighbor (3).
Approach:
To find a peak element in a linked list, we need to identify a node that is greater than or equal to its neighbors. Start from the beginning of the list and compare each node with the next one. If we find a node that is greater than or equal to the next node, it’s a peak, return it.
Notice, we only check the current node with next node, why not with previous node? Because, in the previouse iteration we have already checked the same and if prev was not peak it means it must be smaller than current element).
If we reach the end of the list without finding such a node, check if the last node is greater than or equal to the previous node; if so, it’s a peak.
Below is the implementation of the above approach:
C++
// C++ implementation to find the peak
// element in the Linked List
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to find a peak element in linked list
int findPeakElement(Node* head) {
// If the list is empty, return -1
if (!head) return -1;
// Pointer to store the previous node
Node* prev = nullptr;
// Traverse the list to find the peak element
while (head->next) {
// If the current node is a peak
// then return its value
if (head->data >= head->next->data)
return head->data;
// Update the previous node pointer
prev = head;
// Move to the next node
head = head->next;
}
// If the last node is greater than or equal
// to the previous node, it's a peak
int peakEle;
if (prev && head->data >= prev->data)
peakEle = head->data;
return peakEle;
}
int main() {
// Create a linked list with values:
// 1 -> 2 -> 3 -> 4
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
// Find the peak element in the linked list
int peakElement = findPeakElement(head);
cout << peakElement << endl;
return 0;
}
C
// C implementation to find the peak
// element in the Linked List
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to find a peak element in linked list
int findPeakElement(Node* head) {
// If the list is empty, return -1
if (!head) return -1;
// Pointer to store the previous node
Node* prev = NULL;
// Traverse the list to find the peak element
while (head->next) {
// If the current node is a peak
// then return its value
if (head->data >= head->next->data)
return head->data;
// Update the previous node pointer
prev = head;
// Move to the next node
head = head->next;
}
// If the last node is greater than or equal
// to the previous node, it's a peak
int peakEle = head->data;
if (prev && head->data >= prev->data)
peakEle = head->data;
return peakEle;
}
int main() {
// Create a linked list with values:
// 1 -> 2 -> 3 -> 4
Node* head = (Node*)malloc(sizeof(Node));
head->data = 1;
head->next = (Node*)malloc(sizeof(Node));
head->next->data = 2;
head->next->next = (Node*)malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = (Node*)malloc(sizeof(Node));
head->next->next->next->data = 4;
head->next->next->next->next = NULL;
// Find the peak element in the linked list
int peakElement = findPeakElement(head);
printf("%d\n", peakElement);
return 0;
}
Java
// Java implementation to find the peak
// element in the Linked List
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
// Function to find a peak element in linked list
class GfG {
static int findPeakElement(Node head) {
// If the list is empty, return -1
if (head == null) return -1;
// Pointer to store the previous node
Node prev = null;
// Traverse the list to find the peak element
while (head.next != null) {
// If the current node is a peak
// then return its value
if (head.data >= head.next.data)
return head.data;
// Update the previous node pointer
prev = head;
// Move to the next node
head = head.next;
}
// If the last node is greater than or equal
// to the previous node, it's a peak
int peakEle = head.data;
if (prev != null && head.data >= prev.data)
peakEle = head.data;
return peakEle;
}
public static void main(String[] args) {
// Create a linked list with values:
// 1 -> 2 -> 3 -> 4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
// Find the peak element in the linked list
int peakElement = findPeakElement(head);
System.out.println(peakElement);
}
}
Python
# Python implementation to find the peak
# element in the Linked List
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Function to find a peak element in linked list
def find_peak_element(head):
# If the list is empty, return -1
if not head:
return -1
# Pointer to store the previous node
prev = None
# Traverse the list to find the peak element
while head.next:
# If the current node is a peak
# then return its value
if head.data >= head.next.data:
return head.data
# Update the previous node pointer
prev = head
# Move to the next node
head = head.next
# If the last node is greater than or equal
# to the previous node, it's a peak
peak_ele = head.data
if prev and head.data >= prev.data:
peak_ele = head.data
return peak_ele
# Create a linked list with values:
# 1 -> 2 -> 3 -> 4
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
# Find the peak element in the linked list
peak_element = find_peak_element(head)
print(peak_element)
C#
// C# implementation to find the peak
// element in the Linked List
using System;
public class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
// Function to find a peak element in linked list
class GfG {
static int FindPeakElement(Node head) {
// If the list is empty, return -1
if (head == null) return -1;
// Pointer to store the previous node
Node prev = null;
// Traverse the list to find the peak element
while (head.next != null) {
// If the current node is a peak
// then return its value
if (head.data >= head.next.data)
return head.data;
// Update the previous node pointer
prev = head;
// Move to the next node
head = head.next;
}
// If the last node is greater than or equal
// to the previous node, it's a peak
int peakEle = head.data;
if (prev != null && head.data >= prev.data)
peakEle = head.data;
return peakEle;
}
public static void Main() {
// Create a linked list with values:
// 1 -> 2 -> 3 -> 4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
// Find the peak element in the linked list
int peakElement = FindPeakElement(head);
Console.WriteLine(peakElement);
}
}
JavaScript
// JavaScript implementation to find the peak
// element in the Linked List
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Function to find a peak element in linked list
function findPeakElement(head) {
// If the list is empty, return -1
if (!head) return -1;
// Pointer to store the previous node
let prev = null;
// Traverse the list to find the peak element
while (head.next) {
// If the current node is a peak
// then return its value
if (head.data >= head.next.data)
return head.data;
// Update the previous node pointer
prev = head;
// Move to the next node
head = head.next;
}
// If the last node is greater than or equal
// to the previous node, it's a peak
let peakEle = head.data;
if (prev && head.data >= prev.data)
peakEle = head.data;
return peakEle;
}
// Create a linked list with values:
// 1 -> 2 -> 3 -> 4
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
// Find the peak element in the linked list
let peakElement = findPeakElement(head);
console.log(peakElement);
Time Complexity: O(N) where N is the number of elements in the linked list.
Auxiliary Space: O(1)
Similar Reads
Find unique elements in linked list Given a linked list. We need to find unique elements in the linked list i.e, those elements which are not repeated in the linked list or those elements whose frequency is 1. If No such elements are present in list so Print " No Unique Elements". Examples: Input : 1 -> 4 -> 4 -> 2 -> 3 -
7 min read
Majority element in a linked list Given a linked list, find majority element. An element is called Majority element if it appears more than or equal to n/2 times where n is total number of nodes in the linked list. Examples: Input : 1->2->3->4->5->1->1->1->NULL Output : 1 Explanation 1 occurs 4 times Input :1
14 min read
Find the Second Largest Element in a Linked List Given a Linked list of integer data. The task is to write a program that efficiently finds the second largest element present in the Linked List. Examples: Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1 Output : The second largest element is 34. Input : List = 10 -> 5 -> 10 Outpu
9 min read
find N largest elements from a Linked list Given a Linked list of integers, the task is to find N largest elements in the Linked List. Examples: Input: [4, 5, 1, 2, 9], N = 2Output: [9, 5] Input: [81, 52, 45, 10, 3, 2, 96], N = 3Output: [ 96, 81, 52] Method 1 : Brute ForceApproach: To solve the problem follow the below idea: The idea is to S
15+ min read
Second Smallest Element in a Linked List Given a Linked list of integer data. The task is to write a program that efficiently finds the second smallest element present in the Linked List. Examples: Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1Output : The second smallest element is 10.Input : List = 10 -> 5 -> 10Output
15+ min read