You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
/**
|
|
|
|
* File: ListNode.js
|
|
|
|
* Created Time: 2022-12-12
|
|
|
|
* Author: IsChristina (christinaxia77@foxmail.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition for a singly-linked list node
|
|
|
|
*/
|
|
|
|
class ListNode {
|
|
|
|
val;
|
|
|
|
next;
|
|
|
|
constructor(val, next) {
|
|
|
|
this.val = (val === undefined ? 0 : val);
|
|
|
|
this.next = (next === undefined ? null : next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a linked list with an array
|
|
|
|
* @param arr
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
arrToLinkedList(arr) {
|
|
|
|
const dum = new ListNode(0);
|
|
|
|
const head = dum;
|
|
|
|
for (const val of arr) {
|
|
|
|
head.next = new ListNode(val);
|
|
|
|
head = head.next;
|
|
|
|
}
|
|
|
|
return dum.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list node with specific value from a linked list
|
|
|
|
* @param head
|
|
|
|
* @param val
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
getListNode(head, val) {
|
|
|
|
while (head !== null && head.val !== val) {
|
|
|
|
head = head.next;
|
|
|
|
}
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ListNode
|