Quick actions

cmd+k|ctrl+k

Navigation

Languages

LinkedList Solution

Snippet info

Language

JavaScript

Visibility

public

Author

ryanlove64

Created

2019-06-05T19:22:50Z

Updated

2019-07-10T03:35:15Z



class Ourlist{
  constructor(value){
   this.head = {
         value: value,
         next: null,

   }
   this.tail = this.head;
   this.length = 1;

  }

  append(value){
    //code here
    
    const newNode ={
     value : value,
     next : null
    };
     this.tail.next = newNode;
     this.tail = newNode;
     this.length ++;
     return this;
    
   
  }
  

  PrintTree(temp_head){
     // 
       this.tail = this.head;
      
       while(this.tail.next !== null){
           
         console.log(this.tail.next);
          this.tail = this.tail.next;
       }
      
      
  }
    
    Reverse(){
   
        if(!this.head.next){
            return this.head;
        }
        let first = this.head;
        this.tail = this.head
        let second = first.next;
        while(second){
            const temp = second.next;
            second.next = first;
            first = second;
            second = temp;
        }
        this.head.next = null;
        this.head = first;
        return this.PrintTree(this.head);
    }
    
    
}

const LinkedList = new Ourlist(10);
let temp;
LinkedList.append(10);
LinkedList.append(16);
LinkedList.append(5);
LinkedList.append(22);
LinkedList.append(32);


LinkedList.PrintTree(temp);
LinkedList.Reverse(LinkedList);
//console.log(LinkedList);
INFO