题目:https://leetcode.cn/problems/design-linked-list/description/
设计链表
TS版
class MyListNode {
public val: number;
public next: MyListNode | null;
constructor(val?: number, next?: MyListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}
class MyLinkedList {
// 记录链表长度
private size: number;
private head: MyListNode | null;
private tail: MyListNode | null;
constructor() {
this.size = 0;
this.head = null;
this.tail = null;
}
getNode(index: number) {
let count = 0;
let curNode = new MyListNode(0, this.head);
while(count <= index) {
curNode = curNode.next;
count ++;
}
return curNode;
}
get(index: number): number {
// console.log(this.head, this.tail);
// 索引无效的情况
if (index < 0 || index >= this.size) {
return -1;
}
let curNode = this.getNode(index);
return curNode.val;
}
addAtHead(val: number): void {
const newNode = new MyListNode(val, this.head);
this.head = newNode;
if(!this.tail) {
this.tail = newNode;
}
this.size++;
}
addAtTail(val: number): void {
const newNode = new MyListNode(val, null);
if (this.tail) {
this.tail.next = newNode;
} else {
this.head = newNode;
}
this.tail = newNode;
this.size++;
}
addAtIndex(index: number, val: number): void {
if (index === this.size) {
this.addAtTail(val);
return;
}
if (index > this.size) {
return;
}
if (index <= 0) {
this.addAtHead(val);
return;
}
const curNode = new MyListNode(val, null);
const preNode = this.getNode(index - 1);
curNode.next = preNode.next;
preNode.next = curNode;
this.size++;
}
deleteAtIndex(index: number): void {
if (index < 0 || index >= this.size) {
return;
}
if (index === 0) {
this.head = this.head.next;
if (index === this.size - 1) {
this.tail = null;
}
this.size--;
return;
}
const preNode = this.getNode(index - 1);
preNode.next = preNode.next.next;
if (index === this.size - 1) {
this.tail = preNode;
}
this.size--;
}
}
JS版
class LinkNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
var MyLinkedList = function() {
this._size = 0;
this._tail = null;
this._head = null;
};
MyLinkedList.prototype.getNode = function(index) {
// 虚拟头节点
let cur = new LinkNode(0, this._head);
while(index-- >= 0) {
cur = cur.next;
}
return cur;
}
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this._size) return -1;
return this.getNode(index).val;
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
const node = new LinkNode(val, this._head);
this._head = node;
this._size++;
if (!this._tail) {
this._tail = node;
}
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
const node = new LinkNode(val, null);
this._size++;
if(this._tail) {
this._tail.next = node;
this._tail = node;
return;
}
this._tail = node;
this._head = node;
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
if(index > this._size) return;
if(index <= 0) {
this.addAtHead(val);
return;
}
if(index === this._size) {
this.addAtTail(val);
return;
}
// 获取目标节点的上一个的节点
const node = this.getNode(index - 1);
node.next = new LinkNode(val, node.next);
this._size++;
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if(index < 0 || index >= this._size) return;
if(index === 0) {
this._head = this._head.next;
// 如果删除的这个节点同时是尾节点,要处理尾节点
if(index === this._size - 1){
this._tail = this._head
}
this._size--;
return;
}
// 获取目标节点的上一个的节点
const node = this.getNode(index - 1);
node.next = node.next.next;
// 处理尾节点
if(index === this._size - 1) {
this._tail = node;
}
this._size--;
};