mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	Add JavaScript code to docs (Chapter of linked_list)
This commit is contained in:
		@ -57,7 +57,13 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title=""
 | 
					    ```js title=""
 | 
				
			||||||
 | 
					    /* 链表结点结构体 */
 | 
				
			||||||
 | 
					    class ListNode {
 | 
				
			||||||
 | 
					        constructor(val, next) {
 | 
				
			||||||
 | 
					            this.val = val === undefined ? 0 : val;
 | 
				
			||||||
 | 
					            this.next = next === undefined ? null : next;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -154,7 +160,18 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title=""
 | 
					    ```js title=""
 | 
				
			||||||
 | 
					    /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
 | 
				
			||||||
 | 
					    // 初始化各个结点
 | 
				
			||||||
 | 
					    const n0 = new ListNode(1);
 | 
				
			||||||
 | 
					    const n1 = new ListNode(3);
 | 
				
			||||||
 | 
					    const n2 = new ListNode(2);
 | 
				
			||||||
 | 
					    const n3 = new ListNode(5);
 | 
				
			||||||
 | 
					    const n4 = new ListNode(4);
 | 
				
			||||||
 | 
					    // 构建引用指向
 | 
				
			||||||
 | 
					    n0.next = n1;
 | 
				
			||||||
 | 
					    n1.next = n2;
 | 
				
			||||||
 | 
					    n2.next = n3;
 | 
				
			||||||
 | 
					    n3.next = n4;
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -264,7 +281,23 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title=""
 | 
					    ```js title=""
 | 
				
			||||||
 | 
					    /* 在链表的结点 n0 之后插入结点 P */
 | 
				
			||||||
 | 
					    function insert(n0, P) {
 | 
				
			||||||
 | 
					        const n1 = n0.next;
 | 
				
			||||||
 | 
					        n0.next = P;
 | 
				
			||||||
 | 
					        P.next = n1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 删除链表的结点 n0 之后的首个结点 */
 | 
				
			||||||
 | 
					    function remove(n0) {
 | 
				
			||||||
 | 
					        if (!n0.next) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        // n0 -> P -> n1
 | 
				
			||||||
 | 
					        const P = n0.next;
 | 
				
			||||||
 | 
					        const n1 = P.next;
 | 
				
			||||||
 | 
					        n0.next = n1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -353,7 +386,16 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title=""
 | 
					    ```js title=""
 | 
				
			||||||
 | 
					    /* 访问链表中索引为 index 的结点 */
 | 
				
			||||||
 | 
					    function access(head, index) {
 | 
				
			||||||
 | 
					        for (let i = 0; i < index; i++) {
 | 
				
			||||||
 | 
					            if (!head) {
 | 
				
			||||||
 | 
					                return null;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            head = head.next;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return head;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -444,7 +486,18 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title=""
 | 
					    ```js title=""
 | 
				
			||||||
 | 
					    /* 在链表中查找值为 target 的首个结点 */
 | 
				
			||||||
 | 
					    function find(head, target) {
 | 
				
			||||||
 | 
					        let index = 0;
 | 
				
			||||||
 | 
					        while (head !== null) {
 | 
				
			||||||
 | 
					            if (head.val === target) {
 | 
				
			||||||
 | 
					                return index;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            head = head.next;
 | 
				
			||||||
 | 
					            index += 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return -1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -528,7 +581,14 @@ comments: true
 | 
				
			|||||||
=== "JavaScript"
 | 
					=== "JavaScript"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ```js title=""
 | 
					    ```js title=""
 | 
				
			||||||
 | 
					    /* 双向链表结点类 */
 | 
				
			||||||
 | 
					    class ListNode {
 | 
				
			||||||
 | 
					        constructor(val, next, prev) {
 | 
				
			||||||
 | 
					            this.val = val  ===  undefined ? 0 : val;        // 结点值
 | 
				
			||||||
 | 
					            this.next = next  ===  undefined ? null : next;  // 指向后继结点的引用
 | 
				
			||||||
 | 
					            this.prev = prev  ===  undefined ? null : prev;  // 指向前驱结点的引用
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -539,7 +599,7 @@ comments: true
 | 
				
			|||||||
        val: number;
 | 
					        val: number;
 | 
				
			||||||
        next: ListNode | null;
 | 
					        next: ListNode | null;
 | 
				
			||||||
        prev: ListNode | null;
 | 
					        prev: ListNode | null;
 | 
				
			||||||
        constructor(val?: number, next?: ListNode | null) {
 | 
					        constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {
 | 
				
			||||||
            this.val = val  ===  undefined ? 0 : val;        // 结点值
 | 
					            this.val = val  ===  undefined ? 0 : val;        // 结点值
 | 
				
			||||||
            this.next = next  ===  undefined ? null : next;  // 指向后继结点的引用
 | 
					            this.next = next  ===  undefined ? null : next;  // 指向后继结点的引用
 | 
				
			||||||
            this.prev = prev  ===  undefined ? null : prev;  // 指向前驱结点的引用
 | 
					            this.prev = prev  ===  undefined ? null : prev;  // 指向前驱结点的引用
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user