Created
October 2, 2017 12:25
-
-
Save dhavaljardosh/d27b9273f4ac276e06c3ec3e859e11be to your computer and use it in GitHub Desktop.
Reverse a Linked List created by dhavaljardosh1 - https://repl.it/Lvv8/0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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