In [53]:
const myStack = []
myStack.push(1)
myStack.pop()
Out[53]:
In [54]:
const myQ = []
myQ.push(1)
myQ.push(2)
myQ.shift()
myQ
Out[54]:
In [41]:
const createSLL = () => {
let head = null
const getHead = () => head
const push = value => {
let node = {value, next: null}
if (!head) {
head = node
}
else {
let current = head
while (current.next) {
current = current.next
}
current.next = node
}
}
return {getHead, push}
}
In [42]:
var sll = createSLL()
In [43]:
sll.push(3);
sll.push(4);
sll.push(5);
In [44]:
sll.getHead()
Out[44]:
In [45]:
var createDLL = () => {
let head = null
const getHead = () => head
const push = value => {
let current = head
let previous = head
if (!head) {
head = {value, previous: null, next: null}
}
else {
while (current && current.next) {
previous = current
current = current.next
}
current.next = {value, previous: current, next: null}
}
}
return {getHead, push}
}
In [47]:
var dll = createDLL()
dll.push(2)
dll.push(3)
dll.push(4)
dll.push(5)
In [51]:
dll.getHead().next.previous
Out[51]:
In [ ]:
var ListNode = val => {
let value = val
let next = null
const getValue = () => val
const setValue = newVal => {
value = newVal
}
const getNext = () => next
const setNext = nextNode => {
next = nextNode
}
const appendEnd = endVal => {}
return {getValue, setValue, getNext, setNext}
}
In [ ]:
var TreeNode = val => {
let value = val
let children = []
const getValue = () => value
const getChildren = () => children
const setValue = newVal => {value = newVal}
const addChild = childNode => {children.push(childNode)}
return{getValue, getChildren, setValue, addChild}
}
In [1]:
var TreeNode = val => {
let value = val
let left = null
let right = null
const getValue = () => value
const setValue = newVal => {value = newVal}
const getLeft = () => left
const getRight = () => right
const setLeft = Node => {left = Node}
const setRight = Node => {right = Node}
return{getValue, setValue, getLeft, getRight, setLeft, setRight}
}
In [5]:
var IOT = node => {
if (node) {
IOT(node.getLeft())
console.log(node.getValue())
IOT(node.getRight())
}
}
In [6]:
var PreOT = node => {
if (node) {
console.log(node.getValue())
PreOT(node.getLeft())
PreOT(node.getRight())
}
}
In [7]:
var PostOT = node => {
if (node) {
PostOT(node.getLeft())
PostOT(node.getRight())
console.log(node.getValue())
}
}
In [11]:
var myNode = TreeNode(10)
myNode.setLeft(TreeNode(5))
myNode.getLeft().setLeft(TreeNode(3))
myNode.getLeft().setRight(TreeNode(7))
myNode.setRight(TreeNode(20))
In [12]:
IOT(myNode)
In [13]:
PreOT(myNode)
In [14]:
PostOT(myNode)
In [7]:
var twoSum = (nums, target) => {
for (const i of [...Array(nums.length).keys()]) {
if (nums.includes(target - nums[i])) {
const j = +Object.keys(nums).find(key => nums[key] == target - nums[i] && key != i)
if (j) {
return [i, j]
}
}
}
return "not found"
}
In [20]:
function ListNode(val) {
this.val = val;
this.next = null;
}
In [21]:
function appendAtEnd(Node, val) {
if (!Node.next) {
Node.next = ListNode(value)
}
else {
appendAtEnd(Node.next, val)
}
}
In [24]:
var list1 = ListNode(2)
In [26]:
list1
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [86]:
function foo() {
let a = 2
function bar() {
console.log(a)
}
return bar
}
In [87]:
var baz = foo()
In [88]:
baz()
In [ ]:
In [93]:
var foo = () => {
let a = 2
var bar = () => console.log(a)
return {bar}
}
In [94]:
var baz = foo()
In [96]:
baz.bar()
In [ ]: