Advertisement
apargarg05

Untitled

Mar 14th, 2022
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.33 KB | None | 0 0
  1. class IntNode{
  2.     int value;
  3.     IntNode next;
  4.     
  5.     IntNode(int value, IntNode restOfTheList){
  6.         this.value = value;
  7.         this.next = restOfTheList;
  8.     }
  9. }
  10.  
  11. class Solution {
  12.     IntNode head;
  13.  
  14.     Solution(IntNode head) {
  15.         this.head = head;
  16.     }
  17.  
  18.     static Solution fromArray(int[] arr) {
  19.         // create a linked list with IntNodes
  20.  
  21.         int arrLength = arr.length;
  22.  
  23.         IntNode front = null;
  24.  
  25.         for (int i = arrLength - 1; i >0; i--) {
  26.             front = new IntNode(arr[i], front);
  27.         }
  28.  
  29.         return new Solution(front);
  30.     }
  31.  
  32.     void print() {
  33.         IntNode iterator = head;
  34.         
  35.         while (iterator!=null) {
  36.             System.out.print(iterator.value + " -> ");
  37.             iterator = iterator.next;
  38.         }
  39.         
  40.         System.out.println("null");
  41.     }
  42.  
  43.     Solution drop(int toDrop) {
  44.         IntNode iterator = head;
  45.  
  46.         while (toDrop > 0 && iterator !null) {
  47.             iterator = iterator.next;
  48.             toDrop--;
  49.         }
  50.  
  51.         return new Solution(iterator);
  52.     }
  53.     
  54.     // IntNode reverse(IntNode node){
  55.     //     if(node == null)return null;
  56.         
  57.     //     node.next = reverse(node.next);
  58.     //     node.next.next = node;  
  59.     // }
  60.     
  61.     Solution take(int toTake) {
  62.         if(toTake == 0){
  63.             return new Solution(null);
  64.         }
  65.         
  66.         if(head == null)return this;
  67.  
  68.         IntNode restOfTheList = new Solution(head.next).take(toTake - 1).head;
  69.         return new Solution(new IntNode(head.value, restOfTheList));
  70.         
  71.         // IntNode iterator = head;
  72.         
  73.         // IntNode front = null;
  74.         // // 1 2 3 4 5 ( 3 )
  75.         // while (toTake > 0 && iterator != null) {
  76.         //     front = new IntNode(iterator.value, front);
  77.         //     iterator = iterator.next;
  78.         //     toTake--;
  79.         // } // 3 2 1 
  80.  
  81.         // return new Solution(front);
  82.     }
  83.  
  84.     Solution last(int toTakeLast) {
  85.         // 1 2 3 4 5 6 7 8 null
  86.         //             ^   ^
  87.  
  88.         // 3
  89.         // 1, 3
  90.         //
  91.         
  92.         IntNode iterator = head;
  93.  
  94.         while (toDrop > 0 && iterator !null) {
  95.             iterator = iterator.next;
  96.             toDrop--;
  97.         }
  98.  
  99.         return new Solution(iterator);
  100.     }
  101.  
  102.  
  103.     public static void main(String[] args) {
  104.         Solution empty = Solution.fromArray(new int[] {});
  105.         Solution list1 = Solution.fromArray(new int[] { 123 });
  106.         Solution list2 = Solution.fromArray(new int[] { 12345 });
  107.  
  108.         // empty.drop(1).print(); // null
  109.         // list1.drop(4).print(); // null
  110.         // list1.drop(0).print(); // 1 -> 2 -> 3 -> null
  111.         // list2.drop(2).print(); // 3 -> 4 -> 5 -> null
  112.  
  113.         // empty.take(1).print(); // null
  114.         // list2.take(0).print(); // null
  115.         // list1.take(4).print(); // 1 -> 2 -> 3 -> null
  116.         // list2.take(3).print(); // 1 -> 2 -> 3 -> null
  117.  
  118.         empty.last(1).print(); // null
  119.         list2.last(0).print(); // null
  120.         list1.last(4).print(); // 1 -> 2 -> 3 -> null
  121.         list2.last(3).print(); // 3 -> 4 -> 5 -> null
  122.  
  123.         empty.print(); // null
  124.         list1.print(); // 1 -> 2 -> 3 -> null
  125.         list2.print(); // 1 -> 2 -> 3 -> 4 -> 5 -> null
  126.     }
  127. }
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement