Skip to content

Instantly share code, notes, and snippets.

@dhavaljardosh
Created October 2, 2017 12:25
Show Gist options
  • Save dhavaljardosh/d27b9273f4ac276e06c3ec3e859e11be to your computer and use it in GitHub Desktop.
Save dhavaljardosh/d27b9273f4ac276e06c3ec3e859e11be to your computer and use it in GitHub Desktop.
Reverse a Linked List created by dhavaljardosh1 - https://repl.it/Lvv8/0
class LinkedList{
constructor(){
this.head = null;
this.length=0;
}
add(value){
var node = new Node(value);
if(this.head==null){
this.head = node;
this.length++;
}
else{
var current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
this.length++;
}
}
reverse(){
var current= this.head,previous=null;
while(current)
{
var next = current.next;
current.next = previous;
previous = current;
current = next;
}
return previous;
}
}
class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
var ll = new LinkedList();
ll.add(3);
ll.add(2);
ll.add(7);
ll.add(9);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment