josiftepe

Untitled

Jan 19th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) throws NullPointerException {
  4.         Node list1 = new Node();
  5.         list1.insert_node_at_end(10);
  6.         list1.insert_node_at_end(20);
  7.         list1.insert_node_at_end(30);
  8.         list1.insert_node_at_end(40);
  9.         list1.print();
  10.         Node list2 = new Node();
  11.         list2.insert_node_at_end(10);
  12.         list2.insert_node_at_end(15);
  13.         list2.insert_node_at_end(30);
  14.         list2.insert_node_at_end(40);
  15.         list2.insert_node_at_end(60);
  16.         list2.print();
  17.     Node ret = merge_two_lists(list1, list2);
  18.     ret.print();
  19.     }
  20.     public static Node merge_two_lists(Node tmpA, Node tmpB) {
  21.         Node A = tmpA.head;
  22.         Node B = tmpB.head;
  23.         System.out.println(A.info);
  24.         Node ret = new Node();
  25.         while(A != null && B != null) {
  26.             if(A == null || B == null) {
  27.                 break;
  28.             }
  29.             if(A.info <= B.info) {
  30.                 ret.insert_node_at_end(A.info);
  31.                 A = A.next;
  32.             }
  33.             else {
  34.  
  35.                 ret.insert_node_at_end(B.info);
  36.                B = B.next;
  37.             }
  38.         }
  39.         while(A != null) { // if A is larger than B, continue reversing it
  40.             ret.insert_node_at_end(A.info);
  41.             A = A.next;
  42.         }
  43.         while(B != null) { // if B is larger than A, continue reversing it
  44.             ret.insert_node_at_end(B.info);
  45.             B = B.next;
  46.         }
  47.         return ret;
  48.     }
  49.  
  50. }
  51.  
  52. class Node {
  53.     Node head; // first node in the List
  54.     Node next;
  55.     int info; // storing bunch of integers
  56.     Node() {
  57.         head = null;
  58.         next = null;
  59.     }
  60.     Node(int x) {
  61.         info = x;
  62.         next = null;
  63.     }
  64.     public void insert_node_at_beginning(int x) { // data which the new node will store
  65.         if(head == null) { // empty linked list
  66.             head = new Node(x);
  67.             return;
  68.         }
  69.         Node new_node = new Node(x);
  70.         new_node.next = head;
  71.         head = new_node;
  72.     }
  73.     public void insert_node_at_end(int x) { // at the end insert a node
  74.         Node new_node = new Node(x);
  75.         if(head == null) {
  76.             head = new_node;
  77.             head.next = null;
  78.             return;
  79.         }
  80.         Node tmp = head;
  81.         while(tmp.next != null) {
  82.             tmp = tmp.next;
  83.         }
  84.         tmp.next = new_node;
  85.         new_node.next = null;
  86.     }
  87.     public void delete_node(int x) { // data which should get deleted
  88.         Node tmp = head;
  89.         Node prev = null; // previous node
  90.         while(tmp.info != x) { // iterate through the list till the node which should get deleted
  91.             prev = tmp;
  92.             tmp = tmp.next;
  93.         }
  94.         prev.next = tmp.next;
  95.     }
  96.     public void print() {
  97.         Node tmp = head;
  98.         while(tmp != null) {
  99.             System.out.print(tmp.info + "-> ");
  100.             tmp = tmp.next;
  101.         }
  102.         System.out.println();
  103.     }
  104.  
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment