Skip to content

Instantly share code, notes, and snippets.

@ismkhanh
Last active November 9, 2017 11:30
Show Gist options
  • Select an option

  • Save ismkhanh/46c39c0db88a924545d2d033400b787e to your computer and use it in GitHub Desktop.

Select an option

Save ismkhanh/46c39c0db88a924545d2d033400b787e to your computer and use it in GitHub Desktop.
Adding an item to LinkedList
        /**
         * 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();
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment