reverse a linked list javascript
// 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:
- agrgar atributo con id jquey
- js pow
- difference between express() and express.router()
- react native add link to text
- javascript append element to array
- 0.1 + 0.2 javascript
- expressjs cheat sheet
- jquery tag name
- disable copy past jquery
- add class jquery
- react create list of array in react
- math js
- inmutabilidad javascript
- socket io query
- if(gender.length === 0) javascript
- How do I redirect to another webpage
- Returning Boolean Values from Functions
- javacript detect undefined
- how to print array backwards
- readfilesync
- reduce method javascript
- jquery get data attribute of selected option
- javascript select letter in string
- how to clear pod cache in react native