Files
hello-algo/en/codes/javascript/modules/ListNode.js
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

32 lines
673 B
JavaScript

/**
* File: ListNode.js
* Created Time: 2022-12-12
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/* Linked list node */
class ListNode {
val; // Node value
next; // Reference (pointer) to the next node
constructor(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}
/* Deserialize a list into a linked list */
function arrToLinkedList(arr) {
const dum = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
module.exports = {
ListNode,
arrToLinkedList,
};