Find the largest node in Doubly linked list

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 :

1. Initialize the temp and max pointer to head nodes.
2. Traverse the whole list.
3. if temp’s data is greater than max’s data, then put max = temp.
4. move on next node .

C

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Program to find the largest
   nodes in doubly linked list */
#include <stdio.h>
#include <stdlib.h>
  
// Create a node of the doubly linked list
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};
  
/* UTILITY FUNCTIONS */
/* Function to insert a node at the
beginging of the Doubly Linked List */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  
    /* put in the data */
    new_node->data = new_data;
  
    /* since we are adding at the 
    beginning, prev is always NULL */
    new_node->prev = NULL;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* change prev of head node to new node */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
/* Function to find the largest 
   nodes in Doubly Linked List */
int LargestInDLL(struct Node** head_ref)
{
    struct Node *max, *temp;
  
    /* initialize two pointer temp 
       and max on the head node */
    temp = max = *head_ref;
  
    // traverse the whole doubly linked list
    while (temp != NULL) {
  
        /* if temp's data is greater than
           max's data, then put max = temp
           and return max->data */
        if (temp->data > max->data)
            max = temp;
  
        temp = temp->next;
    }
    return max->data;
}
  
int main()
{
    // Start with the empty list
    struct Node* head = NULL;
  
    // Let us create a linked list
    push(&head, 20);
    push(&head, 14);
    push(&head, 181);
    push(&head, 100);
  
    printf("%d", LargestInDLL(&head));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the largest
// nodes in doubly linked list
class GFG {
  
    // Create node of the doubly linked list
    static class Node {
        int data;
        Node next;
        Node prev;
    };
  
    // UTILITY FUNCTIONS
    // Function to insert a node at the
    // beginging of the Doubly Linked List
    static Node push(Node head_ref, int new_data)
    {
        // allocate node
        Node new_node = new Node();
  
        // put in the data
        new_node.data = new_data;
  
        // since we are adding at the
        // beginning, prev is always null
        new_node.prev = null;
  
        // link the old list off the new node
        new_node.next = (head_ref);
  
        // change prev of head node to new node
        if ((head_ref) != null)
            (head_ref).prev = new_node;
  
        // move the head to point to the new node
        (head_ref) = new_node;
        return head_ref;
    }
  
    // Function to find the largest
    // nodes in Doubly Linked List
    static int LargestInDLL(Node head_ref)
    {
        Node max, temp;
  
        // initialize two pointer temp
        // and max on the head node
        temp = max = head_ref;
  
        // traverse the whole doubly linked list
        while (temp != null) {
  
            // if temp's data is greater than
            // max's data, then put max = temp
            // and return max.data
            if (temp.data > max.data)
                max = temp;
  
            temp = temp.next;
        }
        return max.data;
    }
  
    public static void main(String args[])
    {
        // Start with the empty list
        Node head = null;
  
        // Let us create a linked list
        head = push(head, 20);
        head = push(head, 14);
        head = push(head, 181);
        head = push(head, 100);
  
        System.out.printf("%d", LargestInDLL(head));
    }
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the largest
// nodes in doubly linked list
using System;
  
class GFG {
  
    // Create node of the doubly linked list
    public class Node {
        public int data;
        public Node next;
        public Node prev;
    };
  
    // UTILITY FUNCTIONS
    // Function to insert a node at the
    // beginging of the Doubly Linked List
    static Node push(Node head_ref, int new_data)
    {
        // allocate node
        Node new_node = new Node();
  
        // put in the data
        new_node.data = new_data;
  
        // since we are adding at the
        // beginning, prev is always null
        new_node.prev = null;
  
        // link the old list off the new node
        new_node.next = (head_ref);
  
        // change prev of head node to new node
        if ((head_ref) != null)
            (head_ref).prev = new_node;
  
        // move the head to point to the new node
        (head_ref) = new_node;
        return head_ref;
    }
  
    // Function to find the largest
    // nodes in Doubly Linked List
    static int LargestInDLL(Node head_ref)
    {
        Node max, temp;
  
        // initialize two pointer temp
        // and max on the head node
        temp = max = head_ref;
  
        // traverse the whole doubly linked list
        while (temp != null) {
  
            // if temp's data is greater than
            // max's data, then put max = temp
            // and return max.data
            if (temp.data > max.data)
                max = temp;
  
            temp = temp.next;
        }
        return max.data;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        // Start with the empty list
        Node head = null;
  
        // Let us create a linked list
        head = push(head, 20);
        head = push(head, 14);
        head = push(head, 181);
        head = push(head, 100);
  
        Console.Write("{0}", LargestInDLL(head));
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

 181

Time Complexity: O(n)
Auxiliary Space : O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.