Find the largest node in Doubly linked list
Last Updated :
18 Jan, 2023
Given a doubly-linked list, find the largest node in the doubly linked list.
Examples:
Input: 10->8->4->23->67->88
Largest node is: 88
Output: 88
Input : 34->2->78->18->120->39->7
Largest node is: 120
Output :120
Approach Used:
- Initialize the temp and max pointer to head nodes.
- Traverse the whole list.
- if temp’s data is greater than max’s data, then put max = temp.
- move on to the next node.
Implementation:
C++
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int LargestInDLL( struct Node** head_ref)
{
struct Node *max, *temp;
temp = max = *head_ref;
while (temp != NULL)
{
if (temp->data > max->data)
max = temp;
temp = temp->next;
}
return max->data;
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 14);
push(&head, 181);
push(&head, 100);
cout << LargestInDLL(&head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int LargestInDLL( struct Node** head_ref)
{
struct Node *max, *temp;
temp = max = *head_ref;
while (temp != NULL) {
if (temp->data > max->data)
max = temp;
temp = temp->next;
}
return max->data;
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 14);
push(&head, 181);
push(&head, 100);
printf ( "%d" , LargestInDLL(&head));
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
Node prev;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
static int LargestInDLL(Node head_ref)
{
Node max, temp;
temp = max = head_ref;
while (temp != null ) {
if (temp.data > max.data)
max = temp;
temp = temp.next;
}
return max.data;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 20 );
head = push(head, 14 );
head = push(head, 181 );
head = push(head, 100 );
System.out.printf( "%d" , LargestInDLL(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def pairProduct(head, x):
first = head
second = head
while (second. next ! = None ):
second = second. next
found = False
while (first ! = None and
second ! = None and first ! = second and
second. next ! = first) :
if ((first.data * second.data) = = x) :
found = True
print ( "(" , first.data,
", " , second.data, ")" )
first = first. next
second = second.prev
else :
if ((first.data * second.data) < x):
first = first. next
else :
second = second.prev
if (found = = False ):
print ( "No pair found" )
def push( head, data):
temp = Node( 0 )
temp.data = data
temp. next = temp.prev = None
if (head = = None ):
(head) = temp
else :
temp. next = head
(head).prev = temp
(head) = temp
return head
def LargestInDLL( head_ref):
max = None
temp = None
temp = max = head_ref
while (temp ! = None ):
if (temp.data > max .data):
max = temp
temp = temp. next
return max .data
if __name__ = = "__main__" :
head = None
head = push(head, 20 )
head = push(head, 14 )
head = push(head, 181 )
head = push(head, 100 )
print ( LargestInDLL(head))
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
public Node prev;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
static int LargestInDLL(Node head_ref)
{
Node max, temp;
temp = max = head_ref;
while (temp != null ) {
if (temp.data > max.data)
max = temp;
temp = temp.next;
}
return max.data;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 20);
head = push(head, 14);
head = push(head, 181);
head = push(head, 100);
Console.Write( "{0}" , LargestInDLL(head));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .prev = null ;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
function LargestInDLL(head_ref) {
var max, temp;
temp = max = head_ref;
while (temp != null ) {
if (temp.data > max.data)
max = temp;
temp = temp.next;
}
return max.data;
}
var head = null ;
head = push(head, 20);
head = push(head, 14);
head = push(head, 181);
head = push(head, 100);
document.write(LargestInDLL(head));
</script>
|
Time Complexity: O(n)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...