Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Insert value at specific position.
- *
- * Note: position 0 will update first MyLinkedListRecursive node
- *
- * @param value to add
- * @param position to add value to.
- */
- public void insert(T value, int position) {
- // Base case
- if (isEmpty()) {
- add(value); // When position > size. Add to end of list
- }else if (position == 0) {
- // Change the current MyLinkedListRecursive
- LinkedListRecursive<T> current = new LinkedListRecursive<>();
- current.head = head;
- current.tail = tail;
- this.head = value;
- this.tail = current;
- }else {
- // Recursion
- tail.insert(value, position - 1);
- }
- }
- /* Alternative implementation
- public void insert(T value, int position) {
- if (isEmpty() || position == 0) { //if position > size, add to end of list
- MyLinkedListRecursive<T> add = new MyLinkedListRecursive<>();
- add.tail = this.tail;
- add.head = this.head;
- this.head = value;
- this.tail = add;
- } else {
- tail.insert(value,position - 1);
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement