Advertisement
thewitchking

Untitled

Mar 26th, 2025
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. // Define the structure of a node in the linked list
  5. class Node {
  6.   int data;
  7.   Node next;
  8.   Node(int x)
  9.   {
  10.     data = x;
  11.     next = null;
  12.   }
  13. }
  14.  
  15. class GFG {
  16.  
  17.   // Function to return the reverse of a linked list
  18.   static Node reverseList(Node head)
  19.   {
  20.     Node prev = null;
  21.     Node cur = head;
  22.     Node next_;
  23.     while (cur != null) {
  24.       next_ = cur.next;
  25.       cur.next = prev;
  26.       prev = cur;
  27.       cur = next_;
  28.     }
  29.     return prev;
  30.   }
  31.  
  32.   // Function to return the sum of two linked lists
  33.   static Node addTwoLists(Node l1, Node l2)
  34.   {
  35.     Node dummy = new Node(0);
  36.     Node cur = dummy;
  37.     int carry = 0;
  38.  
  39.     while (l1 != null || l2 != null) {
  40.       int x = l1 != null ? l1.data : 0;
  41.       int y = l2 != null ? l2.data : 0;
  42.       int sum = carry + x + y;
  43.       carry = sum / 10;
  44.       cur.next = new Node(sum % 10);
  45.       cur = cur.next;
  46.       if (l1 != null) {
  47.         l1 = l1.next;
  48.       }
  49.       if (l2 != null) {
  50.         l2 = l2.next;
  51.       }
  52.     }
  53.     if (carry > 0) {
  54.       cur.next = new Node(carry);
  55.     }
  56.     return dummy.next;
  57.   }
  58.  
  59.   // Function to print a linked list
  60.   static void printList(Node head)
  61.   {
  62.     while (head != null) {
  63.       System.out.print(head.data + " ");
  64.       head = head.next;
  65.     }
  66.     System.out.println();
  67.   }
  68.  
  69.   // Function to create a copy of a linked list
  70.   static Node copyList(Node head)
  71.   {
  72.     Node cur = head;
  73.     Node dummy = new Node(0);
  74.     Node copy = dummy;
  75.  
  76.     while (cur != null) {
  77.       copy.next = new Node(cur.data);
  78.       copy = copy.next;
  79.       cur = cur.next;
  80.     }
  81.     return dummy.next;
  82.   }
  83.  
  84.   public static void main(String[] args)
  85.   {
  86.     // Example linked list:
  87.     // 1 -> 2 -> 3 -> 4
  88.     Node head1 = new Node(1);
  89.     head1.next = new Node(2);
  90.     head1.next.next = new Node(3);
  91.     head1.next.next.next = new Node(4);
  92.  
  93.     // Printing original linked list
  94.     System.out.print("Original linked list : ");
  95.     printList(head1);
  96.  
  97.     // Step1 - Make a copy of original linked list
  98.     Node newList = copyList(head1);
  99.  
  100.     // Step2 - Reverse a linked list
  101.     Node head2 = reverseList(newList);
  102.  
  103.     // Printing reversed linked list
  104.     System.out.print("Reversed linked list : ");
  105.     printList(head2);
  106.  
  107.     // Step3 - Addition of a original linked list and
  108.     // its reverse
  109.     Node addition = addTwoLists(head1, head2);
  110.  
  111.     // Step4 - Reverse a addition Linked list
  112.     Node result = reverseList(addition);
  113.  
  114.     // Step5 - Print the resultant linked list
  115.     System.out.print("Resultant linked list : ");
  116.     printList(result);
  117.   }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement