链表

algorithm

# 链表

单向链表

// 单向链表节点类
Class Node() {
    constructor(val) {
        this.val = val;
        this.next = null
    }
}

// 单向链表类
Class ListNode() {
    constructor() {
        this.head = null;
        this.tail = null;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

双向链表

Class Node() {
    constructor(val) {
        this.prev = null;
        this.val = val;
        this.next = null;
    }
}

Class DoubleNode() {
    constructor() {
        this.head = null;
        this.tail = null;
    }
    // 末尾添加元素
    add(item) {
        let node = new Node(item);
        if (!head) {
            this.head = node;
            this.tail = node;
        } else {
            node.prev = this.tail;
            this.tail.next = node;
            this.tail = node;
        }
    }
    // 在某个位置添加元素
    addAt(index, item) {
        let cur = this.head;
        let node = new Node(item);
        // 如果在头部插入
        if(index === 0) {
            node.next = cur;
            cur.prev = node;
            this.head = node;
        } else {
            // 从第一个元素开始加
            let counter = 1;
            while(cur) {
                cur = cur.next;
                if (index === counter) {
                    node.prev = cur.prev;
                    node.next = cur;
                    cur.prev.next = node;
                    cur.prev = node;
                }
                counter++;
            }
        }
    }
    // 删除一个元素
    remove(item) {
        let cur = this.head
        while(cur) {
            // 找到了目标节点
            if(cur === item) {
                // 只有一个元素
                if (cur === this.head && cur === this.tail) {
                    this.head = null
                    this.tail = null
                // 目标为链头
                } else if (cur === this.head) {
                    this.head = this.head.next
                    this.head.prev = null
                } else if (cur === this.tail) {
                    this.tail = this.tail.prev
                    this.tail.next = null
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next
        }
    }
    // 在某个位置中删除元素
    removeAt(index) {
        let cur = this.head
        let counter = 1;
        // 删除链头
        if (index === 0) {
            this.head = this.head.next
            this.head.prev = null
        } else {
            while(cur) {
                cur = cur.next
                if(index === counter) {
                    // 如果在链尾
                    if(cur === this.tail) {
                        this.tail = this.tail.prev
                        this.tail.next = null
                    } else {
                        cur.prev.next = cur.next
                        cur.next.prev = cur.prev
                        break;
                    }
                }
                counter++
            }
        }
    }
    // 翻转链表
    reverse() {
        let cur = this.head
        let prev = null
        while(cur) {
            let next = cur.next
            // 交换指针
            cur.next = prev
            cur.prev = next
            // 向后移动
            prev = cur
            cur = next
        }
        // 当前末尾是prev
        this.tail = this.head
        this.head = prev
    }
    // 交换两个链表元素的位置
    swap(index1, index2) {
        if (index1 > index2) return swap(index2, index1)
        let cur = this.head
        let counter = 0;
        let firstNode
        while (cur!== null) {
            // 找到第一个
            if(counter === index1) {
                firstNode = cur
            // 找到第二个    
            } else (counter === index2) {
                let temp = firstNode.val
                firstNode.val = cur.val
                cur.val = temp
            }
            cur = cur.next
            counter++
        }
        return true
    }
    // 判断是否为空
    isEmpty() {
        return this.length() < 1;
    }
    // 查询链表长度
    length() {
        let cur = this.head;
        let counter = 0;
        while(cur !== null) {
            counter++;
            cur = cur.next;
        }
        return counter;
    }
    // 遍历链表
    traverse(fn) {
        let cur = this.head
        while(cur !== null) {
            fn(cur)
            cur = cur.next
        }
        return true
    }
    // 找到某个节点的索引
    find(item) {
        let cur = this.head
        let counter = 0
        while(cur !== null) {
            if(cur.val === item) {
                return counter
            }
            cur = cur.next
            counter++
        }
        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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

# 延伸题目

  • 2.两数相加问题:题解

  • 65.二进制求和:题解

  • 371.两整数之和:题解

  • 989.数组形式的整数加法:题解

  • 237.删除链表中的节点:题解

  • 203.移除链表元素:题解

  • 206.反转链表:题解

  • 剑指Offer 06. 从尾到头打印链表:题解

  • 剑指Offer 18. 删除链表的节点:题解

  • 剑指Offer 24. 反转链表:题解

更新时间: 2022-03-25 17:04