|
| 1 | +public class LinkedList { |
| 2 | + |
| 3 | + private Node head; // Reference variable pointing to nodes. |
| 4 | + private Node tail; // Reference variable pointing to nodes. |
| 5 | + private int size; // Number of elements in the linked list. |
| 6 | + public LinkedList(){ |
| 7 | + this.size=0; |
| 8 | + } |
| 9 | + |
| 10 | + private class Node { |
| 11 | + |
| 12 | + private int value; |
| 13 | + private Node next; |
| 14 | + |
| 15 | + public Node(int value) { |
| 16 | + this.value = value; |
| 17 | + } |
| 18 | + |
| 19 | + public Node(int value, Node next){ |
| 20 | + this.value = value; |
| 21 | + this.next = next; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Inserting a new node at the starting of the list. |
| 26 | + public void insertFirst(int val){ |
| 27 | + Node node = new Node(val); |
| 28 | + node.next = head; // Assigning the newly created node to point to the previous head. |
| 29 | + head = node; // Changing the head to the current node. |
| 30 | + |
| 31 | + if (tail == null) |
| 32 | + tail = head; // If this is the only node in the list, then tail also points to this address. |
| 33 | + |
| 34 | + size += 1; // Increasing the size of the list coz new element added. |
| 35 | + } |
| 36 | + |
| 37 | + // Inserting a new node at the end of the list. |
| 38 | + public void insertLast(int val){ |
| 39 | + if (tail == null) |
| 40 | + insertFirst(val); |
| 41 | + else { |
| 42 | + Node node = new Node(val); |
| 43 | + tail.next = node; |
| 44 | + tail = node; |
| 45 | + size += 1; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Inserting a new node at any given list |
| 50 | + public void insert(int val, int index){ |
| 51 | + if (index == 0) { |
| 52 | + insertFirst(val); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if(index == size){ |
| 57 | + insertLast(val); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + Node temp = head; |
| 62 | + for (int i = 1; i < index; i++) { |
| 63 | + temp = temp.next; // Reaching before the node where we want to insert our value. |
| 64 | + } |
| 65 | + Node node = new Node(val, temp.next); // Pointing the new node to the next value of the temporary node. |
| 66 | + temp.next = node; |
| 67 | + size++; |
| 68 | + } |
| 69 | + |
| 70 | + // Delete node at the first index. |
| 71 | + public int deleteFirst(){ |
| 72 | + int val = head.value; |
| 73 | + head = head.next; |
| 74 | + if(head==null){ |
| 75 | + tail = null; |
| 76 | + } |
| 77 | + size -= 1; |
| 78 | + return val; |
| 79 | + } |
| 80 | + |
| 81 | + // Delete node at the last index. |
| 82 | + public int deleteLast(){ |
| 83 | + if(size <= 1){ |
| 84 | + return deleteFirst(); |
| 85 | + } |
| 86 | + |
| 87 | + Node secondLast = get(size - 2); |
| 88 | + int value = tail.value; |
| 89 | + tail = secondLast; |
| 90 | + tail.next = null; |
| 91 | + size -= 1; |
| 92 | + return value; |
| 93 | + } |
| 94 | + |
| 95 | + // To delete a node at given index. |
| 96 | + public int delete(int index){ |
| 97 | + if (index == 0) |
| 98 | + return deleteFirst(); |
| 99 | + if (index == size-1) |
| 100 | + return deleteLast(); |
| 101 | + Node prev = get(index - 1); |
| 102 | + Node deleted = prev.next; |
| 103 | + int val = deleted.value; |
| 104 | + prev.next = deleted.next; |
| 105 | + size -= 1; |
| 106 | + return val; |
| 107 | + } |
| 108 | + |
| 109 | + // To return a node that has a particular value. |
| 110 | + public Node find(int value){ |
| 111 | + Node node = head; |
| 112 | + while(node != null){ |
| 113 | + if(node.value == value) |
| 114 | + return node; |
| 115 | + node = node.next; |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + // To return a node at a particular index. |
| 121 | + public Node get(int index){ |
| 122 | + Node node = head; |
| 123 | + for (int i = 0; i < index; i++) { |
| 124 | + node = node.next; |
| 125 | + } |
| 126 | + return node; |
| 127 | + } |
| 128 | + |
| 129 | + // Displaying the entire linked list. |
| 130 | + public void displayList(){ |
| 131 | + Node temp = head; |
| 132 | + while(temp != null) { |
| 133 | + System.out.print(temp.value + "-->"); |
| 134 | + temp = temp.next; |
| 135 | + } |
| 136 | + System.out.println("END"); |
| 137 | + } |
| 138 | +} |
0 commit comments