2022-12-12 21:05:36 +08:00
|
|
|
/**
|
|
|
|
* File: ListNode.js
|
|
|
|
* Created Time: 2022-12-12
|
|
|
|
* Author: IsChristina (christinaxia77@foxmail.com)
|
|
|
|
*/
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 链表节点 */
|
2022-12-12 21:05:36 +08:00
|
|
|
class ListNode {
|
2023-10-01 19:33:53 +08:00
|
|
|
val; // 节点值
|
|
|
|
next; // 指向下一节点的引用(指针)
|
2022-12-12 21:05:36 +08:00
|
|
|
constructor(val, next) {
|
2023-04-17 21:58:11 +08:00
|
|
|
this.val = val === undefined ? 0 : val;
|
|
|
|
this.next = next === undefined ? null : next;
|
2022-12-12 21:05:36 +08:00
|
|
|
}
|
2023-02-05 15:40:30 +08:00
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 将列表反序列化为链表 */
|
2023-02-05 15:40:30 +08:00
|
|
|
function arrToLinkedList(arr) {
|
|
|
|
const dum = new ListNode(0);
|
|
|
|
let head = dum;
|
|
|
|
for (const val of arr) {
|
|
|
|
head.next = new ListNode(val);
|
|
|
|
head = head.next;
|
2022-12-12 21:05:36 +08:00
|
|
|
}
|
2023-02-05 15:40:30 +08:00
|
|
|
return dum.next;
|
|
|
|
}
|
2022-12-12 21:05:36 +08:00
|
|
|
|
2023-02-05 15:40:30 +08:00
|
|
|
module.exports = {
|
|
|
|
ListNode,
|
|
|
|
arrToLinkedList,
|
|
|
|
};
|