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++ 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 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 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 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# 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 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);
Output
4
Time Complexity: O(N) where N is the number of elements in the linked list.
Auxiliary Space: O(1)