/**
* Adds elements at the end of the list
*
* @param value to be added
*/
public void addItem(int value) {
Node newNode = new Node(value);
if (head != null) { //if head is not null, then the list is not empty
Node current = head;
while (current.next != null) { //iterate till the last element
current = current.next;
}
current.next = newNode; //append at the last
} else { //head is null, so the list is empty
head = newNode;
}
System.out.println("Added value: " + value);
print();
}
Last active
November 9, 2017 11:30
-
-
Save ismkhanh/46c39c0db88a924545d2d033400b787e to your computer and use it in GitHub Desktop.
Adding an item to LinkedList
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment