题目
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
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
提示:
所有val值都在 [1, 1000] 之内。
操作次数将在 [1, 1000] 之内。
请不要使用内置的 LinkedList 库。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-linked-list
分析
leecode将要实现的方法已经给出来了,我们只要挨个实现就好了。需要定义一个元素Node,然后MyLinkedList只放入Head和Size,Head是Node类型。我们去实现Get的时候,除了判断边界条件,还要几个要注意的地方。
- 注意指针指向的位置,这里i < index + 1, 这样才能遍历到index的位置。
- 注意添加头指针的时候,是要把Head.Next 指向新节点。
- 注意插入节点的时候,在AddAtIndex里,遍历的时候,要注意的是遍历到index的前一个节点,这样我们才能把新节点插入到这前一个节点后目标位置。
- 总的来说,就是要考虑每一个方法,针对空链表,添加头部,添加尾部,删除尾节点等是否有问题。
链表的设计,要注意的点非常多,边界条件的检查。所以最好是,每一次操作之后,打印出链表看看,插入或者删除的顺序是否正确。
解法
type MyLinkedList struct {
Head *Node
Size int
}
type Node struct {
Val int
Next *Node
}
/** Initialize your data structure here. */
func Constructor() MyLinkedList {
return MyLinkedList{
Head: &Node{},
Size: 0,
}
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
func (this *MyLinkedList) Get(index int) int {
if index < 0 || index >= this.Size {
return -1
}
// fmt.Printf("get size: %d, index:%d\n", this.Size, index)
cur := this.Head
// i=0 时候,需要返回的是head.Next.
for i:=0; i < index+1 ; i++ {
cur = cur.Next
}
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. */
func (this *MyLinkedList) AddAtHead(val int) {
newNode := &Node{
Val: val,
Next: this.Head.Next,
}
this.Head.Next = newNode // 这里要注意,是head next 指向新节点
this.Size++
// this.Print("addAtHead")
}
/** Append a node of value val to the last element of the linked list. */
func (this *MyLinkedList) AddAtTail(val int) {
if this.Size == 0 {
this.AddAtHead(val)
return
}
pred := this.Head
for pred.Next != nil {
pred = pred.Next
}
pred.Next = &Node{
Val:val,
}
this.Size++
// this.Print("addAtTail")
}
/** 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. */
func (this *MyLinkedList) AddAtIndex(index int, val int) {
// index 大于长度,不会被插入
if index > this.Size {
return
}
// index 小于等于0,插入到头部
if index <= 0 {
this.AddAtHead(val)
return
}
// index 等于长度,插入到尾部
if index == this.Size {
this.AddAtTail(val)
return
}
pred := this.Head
// fmt.Printf("head:%v, index:%d\n", pred, index)
// 这应该是停在index的前一个节点
for i:=0; i < index; i++ {
pred = pred.Next
}
newNode := &Node{
Val: val,
Next: pred.Next,
}
pred.Next = newNode
this.Size++
// this.Print("addAtIndex")
}
/** Delete the index-th node in the linked list, if the index is valid. */
func (this *MyLinkedList) DeleteAtIndex(index int) {
if index < 0 || index >= this.Size {
return
}
pred := this.Head
// i 会停在index的前一个位置,如果i是0,能删除吗?
for i:=0; i < index; i++ {
pred = pred.Next
}
pred.Next = pred.Next.Next
this.Size--
// this.Print("deleteAt")
}
func (this *MyLinkedList)Print(msg string){
fmt.Printf("%s: [", msg)
cur := this.Head
for cur!=nil {
fmt.Printf("%v\t", cur.Val)
cur = cur.Next
}
fmt.Println("]")
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Get(index);
* obj.AddAtHead(val);
* obj.AddAtTail(val);
* obj.AddAtIndex(index,val);
* obj.DeleteAtIndex(index);
*/