Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.18 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main{
  4.  
  5.     static class Node{
  6.  
  7.         public int data;
  8.         public Node next;
  9.  
  10.         public Node(int data){
  11.  
  12.             this.data = data;
  13.             this.next = null;
  14.         }
  15.  
  16.         @Override
  17.         public String toString() {
  18.  
  19.             return Integer.toString(data);
  20.         }
  21.     }
  22.  
  23.     static class SinglyLinkedList {
  24.  
  25.         public Node head;
  26.         public Node tail;
  27.  
  28.  
  29.         public SinglyLinkedList() {
  30.  
  31.             this.head = null;
  32.             this.tail = null;
  33.  
  34.         }
  35.  
  36.         public void insertNodeV1(int nodeData) {
  37.  
  38.             Node newNode = new Node(nodeData);
  39.  
  40.             if(this.tail != null && nodeData == this.tail.data) return;
  41.  
  42.             if (this.head == null) {
  43.  
  44.                 this.head = newNode;
  45.  
  46.             } else {
  47.  
  48.                 this.tail.next = newNode;
  49.             }
  50.             this.tail = newNode;
  51.         }
  52.  
  53.         public void insertNodeV2(int nodeData) {
  54.  
  55.  
  56.             Node newNode = new Node(nodeData);
  57.             if (this.head == null) {
  58.  
  59.                 this.head = newNode;
  60.             } else {
  61.  
  62.                 Node currentNode = this.head;
  63.                 while (currentNode.next != null) {
  64.  
  65.                     currentNode = currentNode.next;
  66.                 }
  67.                 currentNode.next = newNode;
  68.             }
  69.         }
  70.  
  71.         private void deleteNext(Node node) {
  72.  
  73.             node.next = node.next.next;
  74.         }
  75.  
  76.  
  77.         public void deleteNode(int position) {
  78.  
  79.             Node curr = head;
  80.             int curr_position = 0;
  81.             if (position == 0) {
  82.  
  83.                 head = head.next;
  84.                 return;
  85.             }
  86.  
  87.             while (curr_position < position - 1) {
  88.  
  89.                 curr = curr.next;
  90.                 curr_position++;
  91.             }
  92.             curr.next = curr.next.next;
  93.         }
  94.  
  95.  
  96.         public void mergeLists(SinglyLinkedList first, SinglyLinkedList second){
  97.  
  98.             Node first_temp = first.head;
  99.             Node second_temp = second.head;
  100.  
  101.             while(first_temp != null || second_temp != null){
  102.  
  103.                 if(first_temp == null){
  104.  
  105.                     insertNodeV1(second_temp.data);
  106.                     second_temp = second_temp.next;
  107.                     continue;
  108.                 }
  109.  
  110.                 if(second_temp == null){
  111.  
  112.                     insertNodeV1(first_temp.data);
  113.                     first_temp = first_temp.next;
  114.                     continue;
  115.                 }
  116.  
  117.  
  118.                 if( first_temp.data < second_temp.data) {
  119.  
  120.                     insertNodeV1(first_temp.data);
  121.                     first_temp = first_temp.next;
  122.                 } else {
  123.  
  124.                     insertNodeV1(second_temp.data);
  125.                     second_temp = second_temp.next;
  126.                 }
  127.             }
  128.         }
  129.  
  130.  
  131.         @Override
  132.         public String toString() {
  133.  
  134.             StringBuilder stringBuilder = new StringBuilder();
  135.  
  136.             Node curr = head;
  137.             while(curr != null){
  138.  
  139.                 stringBuilder.append(curr + "->");
  140.                 curr = curr.next;
  141.             }
  142.             stringBuilder.append("null");
  143.  
  144.             return stringBuilder.toString();
  145.         }
  146.     }
  147.  
  148.  
  149.  
  150.         public static void main(String[] args) {
  151.  
  152.             Scanner reader = new Scanner(System.in);
  153.  
  154.             SinglyLinkedList first = new SinglyLinkedList();
  155.             SinglyLinkedList second = new SinglyLinkedList();
  156.  
  157.             SinglyLinkedList result = new SinglyLinkedList();
  158.  
  159.             int n = reader.nextInt();
  160.  
  161.             for(int i = 0; i < n; ++i){
  162.  
  163.                 first.insertNodeV1(reader.nextInt());
  164.             }
  165.  
  166.             int m = reader.nextInt();
  167.  
  168.             for(int i = 0; i < m; ++i){
  169.  
  170.                 second.insertNodeV1(reader.nextInt());
  171.             }
  172.  
  173.             result.mergeLists(first, second);
  174.  
  175.             System.out.println(result);
  176.  
  177.             Node curr = result.head;
  178.             while(curr != null){
  179.  
  180.                 System.out.printf("%d->", curr.data);
  181.                 curr = curr.next;
  182.             }
  183.             System.out.println("null");
  184.  
  185.         }
  186.  
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement