Find Length of Linked List


public int length(Node head){
		int length=0;
		Node currentNode=head;
		while(currentNode!=null){
			currentNode=currentNode.getNext();
			length++;
		}
		return length;
	}

Recursive:


	public int length(Node current){
		if(current==null) return 0;
		return 1+length(current.getNext());
	}

Leave a Reply

Your email address will not be published. Required fields are marked *