File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Data-Structure/LinkedList/singlyLinkedList Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ class Node {
2+ constructor ( data ) {
3+ this . data = data ;
4+ this . next = null ;
5+ }
6+ }
7+
8+ function reverseList ( list ) {
9+ let currentNode = null ,
10+ nextNode = null ;
11+ while ( list != null ) {
12+ nextNode = list . next ;
13+ list . next = currentNode ;
14+ currentNode = list ;
15+ list = nextNode ;
16+ }
17+ return currentNode ;
18+ }
19+
20+ function displayList ( list ) {
21+ let output = ''
22+ while ( list != null ) {
23+ output += list . data + ' ' ;
24+ list = list . next ;
25+ }
26+ console . log ( output ) ;
27+ }
28+
29+ let list = new Node ( 1 ) ;
30+ list . next = new Node ( 2 ) ;
31+ list . next . next = new Node ( 3 ) ;
32+ list . next . next . next = new Node ( 4 ) ;
33+ list . next . next . next . next = new Node ( 5 ) ;
34+ list . next . next . next . next . next = new Node ( 6 ) ;
35+ let head = reverseList ( list ) ;
36+ displayList ( head ) ;
You can’t perform that action at this time.
0 commit comments