Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Find extra node in the second Linked list
Next article icon

Find extra node in the second Linked list

Last Updated : 15 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 list
Input: L1 = 10 -> 15 -> 5 
L2 = 10 -> 100 -> 15 -> 5 
Output: 100 
 


 


Naive approach: 
 


  • Run nested loops and find the nodes in L2 which is not present in L1. 
     

  • The time complexity of this approach will be O(N2) where N is the length of the linked list. 
     


Efficient approach: 
 


  • If all the nodes of the L1 and L2 are XORed together then every node of A[] will give 0 with its occurrence in L2 and the extra element say X when XORed with 0 will give (X XOR 0) = X which is the result. 
     


Below is the implementation of the above approach:
 

C++
// C++ program to find the  // extra node #include <bits/stdc++.h>  using namespace std;  // Node of the singly linked  // list struct Node {     int data;     Node* next; };  // Function to insert a node at  // the beginning of the singly // Linked List void push(Node** head_ref,           int new_data) {     // allocate node     Node* new_node =      (Node*)malloc(sizeof                   (struct Node));      // put in the data     new_node->data = new_data;      // link the old list of the     // new node     new_node->next = (*head_ref);      // move the head to point to      // the new node     (*head_ref) = new_node; }  int print(Node* head_ref,           Node* head_ref1) {     int ans = 0;      Node* ptr1 = head_ref;     Node* ptr2 = head_ref1;     // Traverse the linked list     while (ptr1 != NULL) {         ans ^= ptr1->data;         ptr1 = ptr1->next;     }     while (ptr2 != NULL) {         ans ^= ptr2->data;         ptr2 = ptr2->next;     }     return ans; }  // Driver program int main() {     // start with the empty list     Node* head1 = NULL;     Node* head2 = NULL;     // create the linked list     // 15 -> 16 -> 7 -> 6 -> 17     push(&head1, 17);     push(&head1, 7);     push(&head1, 6);     push(&head1, 16);      // second  LL     push(&head2, 17);     push(&head2, 7);     push(&head2, 6);     push(&head2, 16);     push(&head1, 15);     int k = print(head1, head2);     cout << k;     return 0; } 
Java
// Java program to find the  // extra node class GFG {     // Node of the singly     // linked list     static class Node {         int data;         Node next;     };      // Function to insert a node at      // the beginning of the singly     // 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;          // link the old list off         // the new node         new_node.next = (head_ref);          // move the head to point         // to the new node         (head_ref) = new_node;         return head_ref;     }      static void extra(Node head_ref1,                        Node head_ref2)     {         int ans = 0;          Node ptr1 = head_ref1;         Node ptr2 = head_ref2;          // Traverse the linked list         while (ptr1 != null) {             ans ^= ptr1.data;             ptr1 = ptr1.next;         }         while (ptr2 != null) {             ans ^= ptr2.data;             ptr2 = ptr2.next;         }          System.out.println(ans);     }      // Driver code     public static void main(String args[])     {         // start with the empty list         Node head1 = null;         Node head2 = null;         // create the linked list         // 15 . 16 . 7 . 6 . 17         head1 = push(head1, 17);         head1 = push(head1, 7);         head1 = push(head1, 6);         head1 = push(head1, 16);         head1 = push(head1, 15);                  // second LL         head2 = push(head2, 17);         head2 = push(head2, 7);         head2 = push(head2, 6);         head2 = push(head2, 16);          extra(head1, head2);     } } 
Python3
# Python3 program to find the  # extra node class Node:              def __init__(self, data):           self.data = data           self.next = next            # Function to insert a node at    # the beginning of the singly  # Linked List   def push( head_ref, new_data) :         # allocate node       new_node = Node(0)          # put in the data       new_node.data = new_data          # link the old list off      # the new node       new_node.next = (head_ref)          # move the head to point     # to the new node       (head_ref) = new_node      return head_ref      def extra(head_ref1, head_ref2) :         ans = 0        ptr1 = head_ref1       ptr2 = head_ref2     # Traverse the linked list       while (ptr1 != None):         # if current node is prime,           # Find sum and product           ans ^= ptr1.data         ptr1 = ptr1.next     while(ptr2 != None):         ans^= ptr2.data         ptr2 = ptr2.next     print(ans)      ## print( "Product = ", prod)      # Driver code      # start with the empty list   head1 = None head2 = None  # create the linked list   # 15 . 16 . 7 . 6 . 17   head1 = push(head1, 17)   head1 = push(head1, 7)   head1 = push(head1, 6)   head1 = push(head1, 16)   head1 = push(head1, 15)    # create the linked list  head2 = push(head2, 17)   head2 = push(head2, 7)   head2 = push(head2, 6)   head2 = push(head2, 16)       extra(head1, head2) 
C#
// C# program to find the extra node using System;  class GFG{      // Node of the singly // linked list class Node {     public int data;     public Node next; };  // Function to insert a node at  // the beginning of the singly // 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;      // Link the old list off     // the new node     new_node.next = (head_ref);      // Move the head to point     // to the new node     (head_ref) = new_node;          return head_ref; }  static void extra(Node head_ref1,                    Node head_ref2) {     int ans = 0;      Node ptr1 = head_ref1;     Node ptr2 = head_ref2;      // Traverse the linked list     while (ptr1 != null)     {         ans ^= ptr1.data;         ptr1 = ptr1.next;     }     while (ptr2 != null)      {         ans ^= ptr2.data;         ptr2 = ptr2.next;     }     Console.WriteLine(ans); }  // Driver code public static void Main(String []args) {          // Start with the empty list     Node head1 = null;     Node head2 = null;          // Create the linked list     // 15 . 16 . 7 . 6 . 17     head1 = push(head1, 17);     head1 = push(head1, 7);     head1 = push(head1, 6);     head1 = push(head1, 16);     head1 = push(head1, 15);              // Second LL     head2 = push(head2, 17);     head2 = push(head2, 7);     head2 = push(head2, 6);     head2 = push(head2, 16);      extra(head1, head2); } }  // This code is contributed by Rajput-Ji 
JavaScript
<script>  // Javascript program to find the  // extra node  // Node of the singly linked  // list class Node {      constructor()     {         this.data = 0;         this.next = null;     } };  // Function to insert a node at  // the beginning of the singly // Linked List function push(head_ref, new_data) {     // allocate node     var new_node = new Node();      // put in the data     new_node.data = new_data;      // link the old list of the     // new node     new_node.next = (head_ref);      // move the head to point to      // the new node     (head_ref) = new_node;     return head_ref; }  function print(head_ref, head_ref1) {     var ans = 0;      var ptr1 = head_ref;     var ptr2 = head_ref1;     // Traverse the linked list     while (ptr1 != null) {         ans ^= ptr1.data;         ptr1 = ptr1.next;     }     while (ptr2 != null) {         ans ^= ptr2.data;         ptr2 = ptr2.next;     }     return ans; }  // Driver program // start with the empty list var head1 = null; var head2 = null;  // create the linked list // 15 . 16 . 7 . 6 . 17 head1 = push(head1, 17); head1 = push(head1, 7); head1 = push(head1, 6); head1 = push(head1, 16);  // second  LL head2 = push(head2, 17); head2 = push(head2, 7); head2 = push(head2, 6); head2 = push(head2, 16); head1 = push(head1, 15); var k = print(head1, head2); document.write( k);  // This code is contributed by noob2000. </script> 

Output: 
15

 

Time Complexity: O(N) 
Space Complexity: O(1)
 


Next Article
Find extra node in the second Linked list

S

ShivaTeja2
Improve
Article Tags :
  • Linked List
  • Data Structures
  • Programming Language
  • C Language
  • DSA
Practice Tags :
  • Data Structures
  • Linked List

Similar Reads

    Find the common nodes in two singly linked list
    Given two linked list, the task is to find the number of common nodes in both singly linked list. Examples: Input: List A = 3 -> 4 -> 12 -> 10 -> 17, List B = 10 -> 4 -> 8 -> 575 -> 34 -> 12 Output: Number of common nodes in both list is = 3 Input: List A = 12 -> 4 -
    15+ min read
    Find the second last node of a linked list in single traversal
    Given a linked list. The task is to find the second last node of the linked list using a single traversal only. Examples: Input : List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output : 4 Input : List = 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL Output : 33 The idea is to traverse t
    7 min read
    Find Middle of the Linked List
    Given a singly linked list, the task is to find the middle of the linked list. If the number of nodes are even, then there would be two middle nodes, so return the second middle node.Example:Input: linked list: 1->2->3->4->5Output: 3 Explanation: There are 5 nodes in the linked list and
    14 min read
    Find first node of loop in a linked list
    Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to find the Starting node of the loop in the linked list if there is no loop in the linked list return -1.Example: Input: Output: 3Exp
    14 min read
    XOR Linked List - Find the middle node
    Given an XOR linked list, the task is to find the middle node of the given XOR linked list. Examples: Input: 4 –> 7 –> 5 Output: 7 Explanation: The middle node of the given XOR list is 7. Input: 4 –> 7 –> 5 –> 1 Output: 7 5 Explanation: The two middle nodes of the XOR linked list with
    14 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