Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /*jshint esnext: true */
- "use strict";
- var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- var Node = function Node(element) {
- _classCallCheck(this, Node);
- this.element = element;
- this.next = null;
- };
- var LinkedList = (function () {
- function LinkedList() {
- _classCallCheck(this, LinkedList);
- this.head = null;
- this.size = 0;
- }
- // adds an element at the end of list
- _createClass(LinkedList, [{
- key: "add",
- value: function add(element) {
- var node = new Node(element);
- var current = undefined;
- // if list is Empty add the element and make it head
- if (this.head == null) this.head = node;else {
- current = this.head;
- // iterate to the end of the list
- while (current.next) {
- current = current.next;
- }
- // add node
- current.next = node;
- }
- this.size++;
- }
- }, {
- key: "reverse",
- value: function reverse() {
- var prev = undefined,
- next = null;
- var curr = this.head;
- while (curr) {
- next = curr.next;
- curr.next = prev;
- prev = curr;
- curr = next;
- }
- this.head = prev;
- }
- }, {
- key: "printList",
- value: function printList() {
- var curr = this.head;
- var str = "";
- while (curr) {
- str += curr.element + " ";
- curr = curr.next;
- }
- console.log(str);
- }
- }]);
- return LinkedList;
- })();
- var ll = new LinkedList();
- ll.add(10);
- ll.add(20);
- ll.add(30);
- ll.add(40);
- ll.printList();
- ll.reverse();
- ll.printList();
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/*jshint esnext: true */
- class Node {
- constructor(element){
- this.element = element;
- this.next = null;
- }
- }
- class LinkedList {
- constructor() {
- this.head = null;
- this.size = 0;
- }
- // adds an element at the end of list
- add(element) {
- let node = new Node(element);
- let current;
- // if list is Empty add the element and make it head
- if (this.head == null) this.head = node;
- else {
- current = this.head;
- // iterate to the end of the list
- while (current.next) {
- current = current.next;
- }
- // add node
- current.next = node;
- }
- this.size++;
- }
- reverse() {
- let prev, next = null;
- let curr = this.head;
- while(curr) {
- next = curr.next;
- curr.next = prev;
- prev = curr;
- curr = next;
- }
- this.head = prev;
- }
- printList() {
- let curr = this.head;
- let str = "";
- while (curr) {
- str += curr.element + " ";
- curr = curr.next;
- }
- console.log(str);
- }
- }
- let ll = new LinkedList();
- ll.add(10);
- ll.add(20);
- ll.add(30);
- ll.add(40);
- ll.printList();
- ll.reverse();
- ll.printList();
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement