reverse a linked list js
// O(n) time & O(n) space
function reverse(head) {
if (!head || !head.next) {
return head;
}
let tmp = reverse(head.next);
head.next.next = head;
head.next = undefined;
return tmp;
}
function LinkedListNode(value) {
this.value = value;
this.next = null;
}
let head = new LinkedListNode(10)
head.next = new LinkedListNode(25)
head.next.next = new LinkedListNode(46)
// Recursive
const reverse = (head) => {
if (!head || !head.next) {
return head;
}
let temp = reverse(head.next);
head.next.next = head;
head.next = undefined;
return temp;
}
head = reverse(head)
Also in JavaScript:
- odoo submit form using js and display data
- react 17 hot reload not working
- jquery select element with data
- javascript floor
- What is strict mode in Java Script ?
- js fucntion return array
- pass argument to event listener javascript
- how to code a minecraft json file mod
- get today date and time in js
- add discord.js library
- angular usehash not working
- chartjs bar chart hover color
- nodejs format text
- object values javascript
- how to make react native dapp truffle
- input not detecting text
- on hover display block jquery
- react native multiple touchableopacity
- extended class call method from super in javascript
- jquery get value of td by class
- using this to get name of the clicked element
- uppy get data to input
- how sent data to dilaog angular material
- javascript base64 decode