Okay so I’m learning Data Structures where my tutorial is following class-based approach. So now I am trying to convert it to functions as I want to practice Linked Lists on Leetcode and Leetcode follows the functional approach. Can anybody help me with the conversion? I need only one method to be converted and I’ll be able to do the rest of them.
Here’s the class-based approach:
class Node {
constructor( val ) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor () {
this.head = null;
this.tail = null;
this.length = 0;
}
push( val ) {
const newNode = new Node( val );
if ( !this.head ) {
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++
return this;
}
}
const list = new SinglyLinkedList();
list.push( 'HELLO' );
list.push( 'WORLD' );
Here’s what I tried so far to achieve function-based approach:
function Node( val ) {
this.val = val;
this.next = null;
}
let head = null;
let tail = null;
let length = 0;
const SinglyLinkedListFunc = function() {
const newNode = new Node( 'HELLO' );
};
SinglyLinkedListFunc()
This is what I could achieve so far. And I’m stuck with it and I don’t know what to do next. Someone please help