Advertisement
YChalk

Merge Lists

Apr 26th, 2022
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8.  
  9. public class Solution {
  10.  
  11.     static class SinglyLinkedListNode {
  12.         public int data;
  13.         public SinglyLinkedListNode next;
  14.  
  15.         public SinglyLinkedListNode(int nodeData) {
  16.             this.data = nodeData;
  17.             this.next = null;
  18.         }
  19.     }
  20.  
  21.     static class SinglyLinkedList {
  22.         public SinglyLinkedListNode head;
  23.         public SinglyLinkedListNode tail;
  24.  
  25.         public SinglyLinkedList() {
  26.             this.head = null;
  27.             this.tail = null;
  28.         }
  29.  
  30.         public void insertNode(int nodeData) {
  31.             SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
  32.  
  33.             if (this.head == null) {
  34.                 this.head = node;
  35.             } else {
  36.                 this.tail.next = node;
  37.             }
  38.  
  39.             this.tail = node;
  40.         }
  41.     }
  42.  
  43.     public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
  44.         while (node != null) {
  45.             bufferedWriter.write(String.valueOf(node.data));
  46.  
  47.             node = node.next;
  48.  
  49.             if (node != null) {
  50.                 bufferedWriter.write(sep);
  51.             }
  52.         }
  53.     }
  54.  
  55.     // Complete the mergeLists function below.
  56.  
  57.     /*
  58.      * For your reference:
  59.      *
  60.      * SinglyLinkedListNode {
  61.      *     int data;
  62.      *     SinglyLinkedListNode next;
  63.      * }
  64.      *
  65.      */
  66.     static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
  67.         SinglyLinkedListNode head;
  68.         SinglyLinkedListNode next = null;
  69.        
  70.         SinglyLinkedListNode firstCurrent = head1;
  71.         SinglyLinkedListNode firstNext = head1.next;
  72.         SinglyLinkedListNode secondCurrent = head2;
  73.         SinglyLinkedListNode secondNext = head2.next;
  74.        
  75.         if (firstCurrent.data <= secondCurrent.data) {
  76.             head = firstCurrent;
  77.             firstCurrent = firstNext;
  78.             firstNext = firstCurrent.next;
  79.             if (firstCurrent == null) {
  80.                 next.next = secondCurrent;
  81.                 return head;
  82.             }
  83.         } else {
  84.             head = secondCurrent;
  85.             secondCurrent = secondNext;
  86.             secondNext = secondCurrent.next;
  87.             if (secondCurrent == null) {
  88.                 next.next = firstCurrent;
  89.                 return head;
  90.             }
  91.         }
  92.                
  93.        
  94.        
  95.         if (firstCurrent.data <= secondCurrent.data) {
  96.             next = firstCurrent;
  97.             firstCurrent = firstNext;
  98.             firstNext = firstCurrent.next;
  99.             if (firstCurrent == null) {
  100.                 next.next = secondCurrent;
  101.                 return head;
  102.             }
  103.         } else {
  104.             next = secondCurrent;
  105.             secondCurrent = secondNext;
  106.             secondNext = secondCurrent.next;
  107.             if (secondCurrent == null) {
  108.                 next.next = firstCurrent;
  109.                 return head;
  110.             }
  111.         }
  112.        
  113.         head.next = next;
  114.        
  115.         while (true) {
  116.             if (firstCurrent.data <= secondCurrent.data) {
  117.                 next.next = firstCurrent;
  118.                 next = next.next;
  119.                 firstCurrent = firstNext;
  120.                 firstNext = firstCurrent.next;
  121.                 if (firstCurrent == null) {
  122.                     next.next = secondCurrent;
  123.                     break;
  124.                 }
  125.             } else {
  126.                 next.next = secondCurrent;
  127.                 next = next.next;
  128.                 secondCurrent = secondNext;
  129.                 secondNext = secondCurrent.next;
  130.                 if (secondCurrent == null) {
  131.                     next.next = firstCurrent;
  132.                     break;
  133.                 }
  134.             }
  135.         }
  136.        
  137.        
  138.         return head;
  139.  
  140.  
  141.     }
  142.  
  143.     private static final Scanner scanner = new Scanner(System.in);
  144.  
  145.     public static void main(String[] args) throws IOException {
  146.         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  147.  
  148.         int tests = scanner.nextInt();
  149.         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  150.  
  151.         for (int testsItr = 0; testsItr < tests; testsItr++) {
  152.             SinglyLinkedList llist1 = new SinglyLinkedList();
  153.  
  154.             int llist1Count = scanner.nextInt();
  155.             scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  156.  
  157.             for (int i = 0; i < llist1Count; i++) {
  158.                 int llist1Item = scanner.nextInt();
  159.                 scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  160.  
  161.                 llist1.insertNode(llist1Item);
  162.             }
  163.          
  164.             SinglyLinkedList llist2 = new SinglyLinkedList();
  165.  
  166.             int llist2Count = scanner.nextInt();
  167.             scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  168.  
  169.             for (int i = 0; i < llist2Count; i++) {
  170.                 int llist2Item = scanner.nextInt();
  171.                 scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  172.  
  173.                 llist2.insertNode(llist2Item);
  174.             }
  175.  
  176.             SinglyLinkedListNode llist3 = mergeLists(llist1.head, llist2.head);
  177.  
  178.             printSinglyLinkedList(llist3, " ", bufferedWriter);
  179.             bufferedWriter.newLine();
  180.         }
  181.  
  182.         bufferedWriter.close();
  183.  
  184.         scanner.close();
  185.     }
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement