题目描述
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
示例:
MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3
提示:
所有值都在 [1, 1000] 之内。
操作次数将在 [1, 1000] 之内。
请不要使用内置的 LinkedList 库。
思路及解答
//链表节点
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
}
}
//我自己的链表
class MyLinkedList {
//这两个变量是在下边很多方法中都要用到的
ListNode head;//定义头节点
int length;//定义链表的长度
/** Initialize your data structure here. */
//构造函数,初始化一个链表,即只有一个val为空的头节点的链表
public MyLinkedList() {
this.head = null;
this.length = 0;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
//得到第index个结点的值,方法是从头结点开始遍历,直到指向第index个结点,然后取出第index个结点的值
public int get(int index) {
//如果index不合法,返回-1
if(index < 0 || index >= this.length)
return -1;
int count = 0;
ListNode cur = head;
while(count < index){
cur = cur.next;
count++;
}
return cur.val;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. 插入头结点,方法是将要插入的结点指向头结点,将新结点指定为头结点*/
public void addAtHead(int val) {
//初始化新结点
ListNode newNode = new ListNode(val);
//将新结点指向头结点
newNode.next = this.head;//新结点的下一个结点为原来的头结点
this.head = newNode;//将newNode设置为头结点
this.length++;//链表长度加1
}
/** Append a node of value val to the last element of the linked list.
在链表的最后的位置添加一个元素。方法是找到最后一个元素,使它指向要添加的新元素(我们知道链表的长度为length)*/
public void addAtTail(int val) {
//如果链表的长度为0,则相当于添加一个头结点
if(this.length == 0){
head = new ListNode(val);
return;//return的方法是结束这个方法的运行
}
//初始化一个新结点
ListNode newNode = new ListNode(val);
int count = 0;
ListNode cur = head;
//找到最后一个结点
while(count < length-1){
cur = cur.next;
count++;
}
cur.next = newNode;//使最后一个结点指向新结点
this.length++;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
在第index个结点之前,插入新结点。如果index等于链表长度,则该结点附加到链表的末尾;如果index大于链表长度,则不会插入结点*/
public void addAtIndex(int index, int val) {
//当index等于链表长度的时候,则将结点插入到链表的末尾
if(index == this.length){
addAtTail(val);
return;
}
//如果index大于链表的长度,则不插入这个新结点
if(index > this.length)
return;
//当index的数值小于链表的长度的时候,将这个新的结点插入到第index个结点之前,首先找到第index个结点的位置
int count = 0;
ListNode cur = head;
ListNode newNode = new ListNode(val);//初始化一个新结点
//找到第index前一个结点的位置
while(count < (index-1)){
cur = cur.next;
count++;
}
newNode.next = cur.next;
cur.next = newNode;
this.length++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
//如果index非法
if(index < 0 || index >= this.length)
return;
//如果要删除的是头结点,直接让head指向下一个结点
if(index == 0)
head = head.next;
//要删除的点不是头结点,并且index合法,方法是找到index结点的前驱结点和后继结点,使前驱结点直接指向后继结点
int count = 0;
ListNode curr = head;
ListNode pre = null;
while(count < index){
pre = curr;
curr = curr.next;
count++;
}
pre.next = curr.next;
this.length--;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/