Update the coding style for JavaScript (#329)

* Fix bug before commit 5eae708

* Update queue.md

* Update the coding style for JavaScript

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
Justin Tse
2023-02-05 15:40:30 +08:00
committed by GitHub
parent 6778557056
commit 6ad8a66a7c
8 changed files with 82 additions and 87 deletions

View File

@ -11,37 +11,41 @@ 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);
let 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;
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
}
module.exports = ListNode
/**
* Generate a linked list with an array
* @param arr
* @return
*/
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;
}
/**
* Get a list node with specific value from a linked list
* @param head
* @param val
* @return
*/
function getListNode(head, val) {
while (head !== null && head.val !== val) {
head = head.next;
}
return head;
}
module.exports = {
ListNode,
arrToLinkedList,
getListNode
};