SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Override
  2.     public void add(int index, E element) {
  3.         int count = 0;
  4.         Node<E> prev = null;
  5.  
  6. //        if (!(index > size())) {
  7.  
  8.         if (index == 0) {
  9.             Node<E> newNode = new Node<>(element);
  10.             newNode.next = first;
  11.             first = newNode;
  12.         } else {
  13.             for (Node<E> temp = first; temp != null; temp = temp.next) {
  14.                 if (count == index) {
  15.                     Node<E> newNode = new Node<>(element);
  16.                     prev.next = newNode;
  17.                     newNode.next = temp;
  18.                 }
  19.                 count++;
  20.                 prev = temp;
  21. //                if (temp.next == null) {
  22. //                    last = temp;
  23. //                }
  24.             }
  25.         }
  26.         if (index == size()) {
  27.             Node<E> newNode = new Node<>(element);
  28.             prev.next = newNode;
  29.             last = newNode;
  30.         }
  31. //        }
  32.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top