Advertisement
amitsen01ei

Reverse Linked List Between a range

Aug 27th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 KB | Source Code | 0 0
  1. import java.util.*;
  2. import java.util.stream.*;
  3.  
  4. class Node<E> {
  5.  
  6.     E element;
  7.  
  8.     Node<E> next;
  9.  
  10.     Node(E element, Node<E> next) {
  11.         this.element = element;
  12.         this.next = next;
  13.     }
  14.  
  15.     Node(E element) {
  16.         this.element = element;
  17.         this.next = null;
  18.     }
  19.  
  20.     @Override
  21.     public String toString() {
  22.         return String.format("{ element : %s }",
  23.                 (element == null ? "null" : element.toString()));
  24.     }
  25. }
  26.  
  27. class MyLinkedList<E> implements Iterable<E> {
  28.  
  29.     Node<E> head;
  30.  
  31.     Node<E> tail;
  32.  
  33.     int size;
  34.  
  35.     MyLinkedList() {
  36.         this.head = null;
  37.         this.tail = null;
  38.         this.size = 0;
  39.     }
  40.  
  41.     void addLast(E value) {
  42.  
  43.         final var newNode = new Node<>(value, null);
  44.  
  45.         if (this.head == null) {
  46.             this.head = newNode;
  47.         } else {
  48.             this.tail.next = newNode;
  49.         }
  50.  
  51.         this.tail = newNode;
  52.         ++this.size;
  53.     }
  54.  
  55.     /**
  56.      * Adds the iterable support for using this custom linked list in an enhanced for loop.
  57.      * @return the Iterable object of this custom linked list.
  58.      */
  59.     @Override
  60.     public Iterator<E> iterator() {
  61.  
  62.         return new Iterator<>() {
  63.  
  64.             Node<E> current = head;
  65.  
  66.             @Override
  67.             public boolean hasNext() {
  68.                 return current != null;
  69.             }
  70.  
  71.             @Override
  72.             public E next() {
  73.                 E value = current.element;
  74.                 current = current.next;
  75.                 return value;
  76.             }
  77.         };
  78.     }
  79.  
  80.     /**
  81.      * Adds Stream API support on this custom implementation of the generic linked list
  82.      * @return the stream object of the custom linked list
  83.      */
  84.     Stream<E> stream() {
  85.         return StreamSupport.stream(spliterator(), false);
  86.     }
  87.  
  88.     /**
  89.      * Prints a linked list in a formatted fashion given its head node. This function relies on
  90.      * the custom implementation of a generic linked list above.
  91.      * @param head the head node of a linked list
  92.      * @param <E> denotes the generic type
  93.      */
  94.     static <E> void print(Node<E> head) {
  95.         var sb = new StringBuilder("[");
  96.         Node<E> current = head;
  97.  
  98.         if (current != null) {
  99.             sb.append(current.element);
  100.             current = current.next;
  101.         }
  102.  
  103.         while (current != null) {
  104.             sb.append(",").append(current.element);
  105.             current = current.next;
  106.         }
  107.  
  108.         sb.append("]");
  109.  
  110.         System.out.println(sb);
  111.     }
  112. }
  113.  
  114. public class ReverseInRange {
  115.  
  116.  
  117.     /**
  118.      * REQUIRES MINIMUM JDK 10 or above
  119.      *
  120.      * <p>
  121.      *     This function reverses a linked list for a given range. The range is assumed to be
  122.      *     1-based, unlike 0-based indexing like array. The function relies on the custom
  123.      *     implementation of a generic linked list above.
  124.      * </p>
  125.      * <p>
  126.      *     Time complexity: In the worst case where we need to reverse the entire list,
  127.      *     the time complexity of this function would be O(N) where N denotes the size of the list
  128.      * </p>
  129.      * <p>
  130.      *     Space complexity: In the worst case, the function uses constant memory. So the space complexity
  131.      *     would be O(1)
  132.      * </p>
  133.      * @param head the head node of the linked list
  134.      * @param left an integer denoting starting position (inclusive)
  135.      * @param right and integer denoting ending position (inclusive)
  136.      * @return the updated head of the reversed linked list
  137.      * @param <E> denotes the generic type
  138.      */
  139.     private static <E> Node<E> reverseInRange(Node<E> head, int left, int right) {
  140.  
  141.         if (left < 1 || right < 1 || left > right) {
  142.             throw new IllegalArgumentException("Invalid range values");
  143.         }
  144.  
  145.         if (head == null || left == right) {
  146.             return head;
  147.         }
  148.  
  149.         var temp = new Node<E>(null);
  150.         temp.next = head;
  151.  
  152.         Node<E> prev = temp;
  153.  
  154.         for (var i = 0; i < left - 1; i++) {
  155.             prev = prev.next;
  156.         }
  157.  
  158.         Node<E> start = prev.next;
  159.         Node<E> then = start.next;
  160.  
  161.         for (var i = 0; i < right - left; i++) {
  162.             start.next = then.next;
  163.             then.next = prev.next;
  164.             prev.next = then;
  165.             then = start.next;
  166.         }
  167.  
  168.         head = temp.next;
  169.  
  170.         return head;
  171.     }
  172.  
  173.     public static void main(String[] args) {
  174.  
  175.         try (var sc = new Scanner(System.in)) {
  176.  
  177.             int n = sc.nextInt();
  178.  
  179.             var linkedList = new MyLinkedList<Integer>();
  180.  
  181.             for (var i = 0; i < n; i++) {
  182.                 linkedList.addLast(sc.nextInt());
  183.             }
  184.  
  185.             int left = sc.nextInt();
  186.             int right = sc.nextInt();
  187.  
  188.             System.out.println("Original List: " + linkedList.stream()
  189.                     .map(Objects::toString)
  190.                     .collect(Collectors.joining(",", "[", "]")));
  191.  
  192.             var head = reverseInRange(linkedList.head, left, right);
  193.  
  194.             System.out.printf("Reversed (From %d To %d): ", left, right);
  195.             MyLinkedList.print(head);
  196.         }
  197.  
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement