链表基本操作Golang版本

73 阅读1分钟
package main

type SingleNode struct {
   Val  int
   Next *SingleNode
}
type ListNode struct {
   dummyHead *SingleNode
   Size      int
}

func Constructor() ListNode {
   return ListNode{}
}

// get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
func (this *ListNode) get(index int) int {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   // 定义遍历指针
   curr := dumyNode.Next
   for index > 0 {
      if curr.Next == nil {
         return -1
      }
      curr = curr.Next
      index--
   }
   if curr != nil {
      return -1
   }
   return curr.Val
}

// 在头部添加节点
func (this *ListNode) addAtHead(val int) {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   // 构造新节点
   newNode := &SingleNode{
      Val:  val,
      Next: dumyNode.Next,
   }
   this.dummyHead = newNode
   this.Size++
}

// 在尾部添加节点
func (this *ListNode) addAtTail(val int) {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   curr := dumyNode
   for curr.Next != nil {
      curr = curr.Next
   }
   // 构造新节点
   curr.Next = &SingleNode{
      Val:  val,
      Next: nil,
   }
   this.dummyHead = dumyNode.Next
   this.Size++
}

// 在指定位置添加节点
func (this *ListNode) addAtIndex(index int, val int) {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   curr := dumyNode
   // 先定位
   for index > 0 {
      index--
      // 如果index 超长度了, 不执行
      if curr.Next == nil {
         return
      }
      curr = curr.Next
   }
   // 构造新节点
   curr.Next = &SingleNode{
      Val:  val,
      Next: curr.Next,
   }
   this.dummyHead = dumyNode.Next
   this.Size++
}

// 删除指定位置的节点
func (this *ListNode) deleteAtIndex(index int) {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   curr := dumyNode
   // 先定位
   for index > 0 {
      index--
      // 如果index 超长度了, 不执行
      if curr.Next == nil {
         return
      }
      curr = curr.Next
   }
   // 构造新节点
   if curr.Next == nil {
      curr.Next = nil
   } else {
      curr.Next = curr.Next.Next
   }
   this.dummyHead = dumyNode.Next
   this.Size--
}

func (this *ListNode) reverse() {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   curr := dumyNode.Next
   //res := &SingleNode{}
   var pre *SingleNode
   //res.Next = pre
   for curr != nil {
      temp := curr.Next
      curr.Next = pre
      pre = curr
      curr = temp
   }
   this.dummyHead = pre
}

// 反转链表(递归方式)
func (this *ListNode) reverseDigui() {
   // 定义虚拟节点
   dumyNode := &SingleNode{}
   dumyNode.Next = this.dummyHead
   curr := dumyNode.Next
   var pre *SingleNode
   this.reverseDiguiHandler(pre, curr)
   this.dummyHead = pre
}
func (this *ListNode) reverseDiguiHandler(pre *SingleNode, curr *SingleNode) *SingleNode {
   if curr == nil {
      // 跳出递归
      return pre
   }
   temp := curr.Next
   curr.Next = pre
   return this.reverseDiguiHandler(curr, temp)
}

// 测试
func main() {
   lnn := &ListNode{
      dummyHead: &SingleNode{
         Val: 9,
         Next: &SingleNode{
            Val: 8,
            Next: &SingleNode{
               Val: 7,
               Next: &SingleNode{
                  Val:  6,
                  Next: nil,
               },
            },
         },
      },
      Size: 0,
   }
   print(lnn)

   myLinkedList := Constructor()
   myLinkedList.addAtHead(1)
   myLinkedList.addAtTail(3)
   myLinkedList.addAtIndex(1, 2) // 链表变为 1->2->3
   myLinkedList.get(1)           // 返回 2
   myLinkedList.deleteAtIndex(1) // 现在,链表变为 1->3
   myLinkedList.get(1)           // 返回 3
   myLinkedList.reverseDigui()   // 翻转

}

/**
                     .::::.
                   .::::::::.
                  :::::::::::
               ..:::::::::::'
            '::::::::::::'
              .::::::::::
         '::::::::::::::..
              ..::::::::::::.
            ::::::::::::::::
             :::::::::::::'        .:::.
            ::::'   ':::::'       .::::::::.
          .::::'      ::::     .:::::::'::::.
         .:::'       :::::  .:::::::::' ':::::.
        .::'        :::::.:::::::::'      ':::::.
       .::'         ::::::::::::::'         ::::.
   ...:::           ::::::::::::'              ::.
  .....':.          ':::::::::'                  ::::..
                     '.:::::'                    ':'..

*/