leetcode 探索 链表 设计链表II双向链表

题目

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-linked-list

分析

要注意的是新增和删除的时候,prev、next的处理。尤其要注意的是,head的处理,引入伪头简化处理。

解法

type MyLinkedList struct {
    Head *Node
    Size int
}

type Node struct {
    Val int
    Next *Node
    Prev *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
    }
    cur := this.Head.Next
    // i=0 时候,需要返回的是head.Next.
    for i:=0; i < index; 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,
        Prev: this.Head,
    }
    if this.Head.Next != nil {
        this.Head.Next.Prev = newNode
    }
    
    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,
        Prev: pred,
    }
    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
    // 这应该是停在index的前一个节点
    for i:=0; i < index; i++ {
        pred = pred.Next
    }
    newNode := &Node{
        Val: val,
        Next: pred.Next,
        Prev: pred,
    }
    if pred.Next != nil {
        pred.Next.Prev = newNode
    }
    
    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
    }
    
    cur := this.Head.Next
    // i 会停在index的要删除的位置 
    for i:=0; i < index; i++ {
        cur = cur.Next
    }
    
    if cur != nil {
        cur.Prev.Next = cur.Next
        if cur.Next != nil {
            cur.Next.Prev = cur.Prev
        }
    }
    
    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);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值