Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Insert a Node after a given node in Doubly Linked List
Next article icon

Find the largest node in Doubly linked list

Last Updated : 18 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 to the next node.

Implementation:

C++
/* C++ Program to find the largest  nodes in doubly linked list */ #include <iostream> using namespace std;  // 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  beginning 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 of 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;  }   // Driver code 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);       cout << LargestInDLL(&head);      return 0;  }   // This code is contributed by shubhamsingh10 
C
/* 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 beginning 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 of 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; } 
Java
// 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     // beginning 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 of 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 
Python3
# Python3 program to find largest  # node in doubly linked list  # Node of the doubly linked list  class Node:           def __init__(self, data):          self.data = data          self.prev = None         self.next = None  # Function to find pair whose product # equal to given value x def pairProduct(head, x):      # Set two pointers,     # first to the beginning of DLL     # and second to the end of DLL.     first = head     second = head     while (second.next != None):         second = second.next      # To track if we find a pair or not     found = False      # The loop terminates when either of two pointers     # become None, or they cross each other (second.next     # == first), or they become same (first == second)     while (first != None and            second != None and first != second and             second.next != first) :                         # pair found         if ((first.data * second.data) == x) :             found = True             print("(" , first.data,                    ", ", second.data, ")")              # move first in forward direction             first = first.next              # move second in backward direction             second = second.prev                  else :             if ((first.data * second.data) < x):                 first = first.next             else:                 second = second.prev          # if pair is not present     if (found == False):         print( "No pair found")  # A utility function to insert a new node at the # beginning of doubly linked list 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      """ Function to find the largest  nodes in Doubly Linked List """ def LargestInDLL( head_ref):      max = None     temp = None      """ initialize two pointer temp      and max on the head node """     temp = max = head_ref      # traverse the whole doubly linked list     while (temp != None):           """ 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 if __name__ == "__main__":       # Start with the empty list     head = None      # Let us create a linked list     head = push(head, 20)     head = push(head, 14)     head = push(head, 181)     head = push(head, 100)      print( LargestInDLL(head))      # This code is contributed by Arnab Kundu 
C#
// 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     // beginning 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 of 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 
JavaScript
<script>     // javascript Program to find the largest     // nodes in doubly linked list         // Create node of the doubly linked list     class Node {         constructor(val) {             this.data = val;             this.prev = null;             this.next = null;         }     }      // UTILITY FUNCTIONS     // Function to insert a node at the     // beginning of the Doubly Linked List     function push(head_ref , new_data) {         // allocate node         var 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 of 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     function LargestInDLL(head_ref) {         var 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;     }           // Start with the empty list     var head = null;      // Let us create a linked list     head = push(head, 20);     head = push(head, 14);     head = push(head, 181);     head = push(head, 100);      document.write(LargestInDLL(head));  // This code contributed by umadevi9616  </script> 

Output
181

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


Next Article
Insert a Node after a given node in Doubly Linked List

S

shrikanth13
Improve
Article Tags :
  • Linked List
  • DSA
  • doubly linked list
Practice Tags :
  • Linked List

Similar Reads

  • 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
  • Insert a Node at the end of Doubly Linked List
    Given a Doubly Linked List, the task is to insert a new node at the end of the linked list. Examples: Input: Linked List = 1 <-> 2 <-> 3, NewNode = 4Output: Linked List = 1 <-> 2 <-> 3 <-> 4 Input: Linked List = NULL, NewNode = 1Output: Linked List = 1 Approach: Inserti
    9 min read
  • Insertion Sort for Doubly Linked List
    Given a doubly linked list, the task is to sort the doubly linked list in non-decreasing order using the insertion sort. Examples: Input: head: 5<->3<->4<->1<->2Output: 1<->2<->3<->4<->5Explanation: Doubly Linked List after sorting using insertion sort
    10 min read
  • Insert a Node after a given node in Doubly Linked List
    Given a Doubly Linked List, the task is to insert a new node after a given node in the linked list. Examples: Input: Linked List = 1 <-> 2 <-> 4, newData = 3, key = 2Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node 3 is inserted after key, that is node 2.
    11 min read
  • Deletion at end (Removal of last node) in a Doubly Linked List
    Given a doubly linked list, the task is to delete the last node of the given linked list. Examples: Input: 1 <-> 2 <-> 3 <-> NULLOutput: 1 <-> 2 <-> NULLExplanation: The last node of the linked list is 3, so 3 is deleted. Input: 15 -> NULLOutput: NULLExplanation: The
    7 min read
  • Find smallest and largest elements in singly linked list
    Given a singly linked list of n nodes, the task is to find the smallest and largest element in linked list. Examples: Input: 15 -> 14 -> 13 -> 22 -> 17Output: 13 22 Explanation: The minimum element in the linked list is 13, and the maximum element is 22. Input: 20 -> 25 -> 23 ->
    7 min read
  • Find the Largest Number Using Four Nodes in a Singly Linked List
    Given a singly linked list, the task is to find the largest number in the linked list using four nodes. Examples: Input: List: 2->5->3->4->6Output: 6543Explanation: Using the four nodes, we traverse the linked list and construct the largest number, which is 6543 Input: List: 1->7->
    12 min read
  • Find extra node in the second Linked list
    Given two Linked list L1 and L2. The second list L2 contains all the nodes of L1 along with 1 extra node. The task is to find that extra node. Examples: Input: L1 = 17 -> 7 -> 6 -> 16 L2 = 17 -> 7 -> 6 -> 16 -> 15 Output: 15 Explanation: Element 15 is not present in the L1 listI
    7 min read
  • Find pairs with given sum in doubly linked list
    Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in a doubly-linked list whose sum is equal to the given value x in sorted order. Examples: Input: Output: (1, 6), (2,5)Explanation: We can see that there are two pairs (1, 6) and (2, 5) with sum 7. Input: Outp
    14 min read
  • Insert a Node before a given node in Doubly Linked List
    Given a Doubly Linked List, the task is to insert a new node before a given node in the linked list. Examples: Input: Linked List = 1 <-> 3 <-> 4, newData = 2, key = 3Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node with data 2 is inserted before the node
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences