<script type= "Text/javascript" ><!-- one-way linkedlist reverse in Javascript -->function node (value) {this.value = value;this.next = null;} Node.prototype.setnext = function (node) {this.next = node;return node;} Node.prototype.printlist = function () {var top = this;while (top) { Console.log (top.value); top = top.next;}} Node.prototype.reverse = function () {var topnode = null;var originaltop = this;var lasttopnode = originaltop;while (Originaltop.next) {topNode = originaltop.next;originaltop.setnext (OriginalTop.next.next); Topnode.setnext (Lasttopnode); Lasttopnode = topnode;} Return topnode;} Var head = new node (1); Head.setnext (New node (2)). Setnext (New node (3)). SetNext ( New node (4)). Setnext (New node (5)); Head.printlist (); hEad = head.reverse (); Head.printlist ();</script>
Unidirectional link List traversal inversion JavaScript implementation