Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

iterate through linked list javascript

class Node {
  constructor(element){
  	this.element = element;
    this.next = null;
  }
}

class LinkedList {
	constructor(){
    	this.head = null;
      	this.size = 0;
    }
  
  	add(element){
    	var node = new Node(element);
      	var current;
      
      	if(this.head == null) this.head = node;
      	else {
        	current = this.head;
          
          	while(current.next){
            	current = current.next
            }
          
          	current.next = node;
        }
      this.size++;
    }
  
  loopAndFindElement(element){
  	var count = 0;
    var current = this.head;
    
    while(current !== null) {
    	if(current.element === element){
        	return count;
        }
      
      	count++;
      
      	current = current.next
    }
    
    return "Element not found || -1";
  }
}
 
PREVIOUS NEXT
Tagged: #iterate #linked #list #javascript
ADD COMMENT
Topic
Name
9+4 =