Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- constructor(data, next) {
- this.data = data;
- this.next = next;
- }
- }
- function createListFromArray(array) {
- let list = null;
- for (const item of array) {
- list = new Node(item, list);
- }
- return list;
- }
- function displayList(list) {
- while (list) {
- console.log(list.data);
- list = list.next;
- }
- }
- function length(head) {
- let length = 0;
- while (head != null) {
- length++;
- head = head.next;
- }
- return length;
- }
- let array = [3,2,1];
- let list = createListFromArray(array);
- console.log(length(list));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement