数据结构算法链表篇之设计链表

设计链表

  1. LeetCode题目链接
  2. 卡尔老师代码随想录讲解
  3. 关键点: 这道题目包含了设计链表的基础API,包括得到链表中相对应索引的值在链表头部添加节点在链表尾部添加节点在给定索引值的元素前面添加节点删除给定索引的节点。在完善这些API要特别注意头部节点、尾部节点、空链表、单节点链表的特殊情况,需要我们重点考虑。 在题目要求之外我们设计了一个 getNode 用于快速获取相应索引的节点方便被设计的 API 调用。
  4. 代码:
function LinkNode (val,next) {
    this.val = (val === undefined ? null : val )
    this.next = (next === undefined ? null : next)
}

var MyLinkedList = function() {
    this._head = null
    this._tail = null
    this._size = 0
};

MyLinkedList.prototype.getNode = function (index) {
    if (index < 0 || index >= this._size) return null
    //设置头节点方便统一对头节点的操作
    let cur = new LinkNode (null,this._head)  
    while (index-- >=0) {
        cur = cur.next
    }
    return cur
}

MyLinkedList.prototype.get = function(index) {
    if (index < 0 || index >= this._size) {
        return -1
    } else {
        return this.getNode(index).val
    }
};

MyLinkedList.prototype.addAtHead = function(val) {
   let newNode = new LinkNode (val,null)
   //考虑空链表情况
   if (this._size == 0) {
       this._head = newNode
       this._tail = newNode
       this._size ++
       return
   }
   newNode.next = this._head
   this._head = newNode
   this._size++
};

MyLinkedList.prototype.addAtTail = function(val) {
   let newNode = new LinkNode (val,null)
   if (this._tail ) {
       this._tail.next = newNode
       this._tail = newNode
   } else {
   	   //空链表情况下添加尾节点
       this._tail = newNode
       this._head = newNode
   }
   this._size++
};

MyLinkedList.prototype.addAtIndex = function(index, val) {
   if(index <= 0) {
   	   //直接调用已经写好的*API*
       this.addAtHead(val)
   } else if (index == this._size) {
       this.addAtTail(val)
   } else if (index < this._size) {
       let cur = this.getNode(index-1)
       let newNode = new LinkNode (val,null)
       newNode.next = cur.next
       cur.next = newNode
       this._size++
   }
}

MyLinkedList.prototype.deleteAtIndex = function(index) {
   //索引值要在合法范围内
   if(index >=0 && index < this._size) {
       if (this._size > 1) {
           if(index == 0 ){
               //当要删除的是第一个元素时要修改_head指针
               this._head = this._head.next
           } else {
               let cur = this.getNode(index-1)
               if(index == this._size-1) {
                   //当删除的元素为尾部元素时特殊处理
                   cur.next = null
                   this._tail =cur
               }else {
                   cur.next = cur.next.next 
               }  
           } 
           this._size--
       } else {
       	   //当链表为空和只有一个元素时直接设定
           this._head = null
           this._tail = null
           this._size = 0
       }
   }
};
  1. 注意:
  • 在添加头部和尾部节点时考虑空链表的特殊情况
  • 在删除给定索引值的节点时,考虑空链表和单节点情况,考虑删除的节点是头元素和尾元素的情况
  1. 时间复杂度分析:
    主要考察设计链表内容,不进行算法分析
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值