# 练习

# LeetCode.237.删除链表中的节点

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} node
 * @return {void} Do not return anything, modify node in-place instead.
 */
var deleteNode = function(node) {
    node.val = node.next.val;
    node.next = node.next.next;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# LeetCode.206. 反转链表

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    if(!head){
        return null
    }
    let h = head.next;
    let pre = head;
    pre.next = null;
    while(h){
        let t = h.next;
        h.next = pre;
        pre = h;
        h = t;
    }
    return pre
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# LeetCode.2. 两数相加

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
    
    let res = new ListNode(0)
    let l3 = res;
    let p = 0;
    while (l1 || l2 || p) {
        let n1 = 0;
        let n2 = 0;
        if (l1) {
            n1 = l1.val;
            l1 = l1.next
        }
        if (l2) {
            n2 = l2.val;
            l2 = l2.next;
        }

        let r = n1 + n2 + p;
        p = parseInt(r / 10)
        r = parseInt(r % 10)
        l3.next = new ListNode(r);
        l3 = l3.next
    }
    return res.next;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# LeetCode.83. 删除排序链表中的重复元素

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function (head) {
    if (!head)
        return head

    let res = head
    let pre = head;
    let next = head.next
    while (next) {
        if (next.val == pre.val) {
            pre.next = next.next
        }else{
            pre = next
        }
        next = next.next
    }
    return res
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# LeetCode.141.环形链表

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
    if (!head) {
        return false
    }
    let map = new Map();
    while (head) {
        if (map.has(head)) {
            return true
        } else {
            map.set(head,true)
        }
        head = head.next
    }
    return false
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27