单链表
| package linkedList |
| |
| |
| type ListNode struct { |
| Val int |
| Next *ListNode |
| } |
| |
| type MyLinkedList struct { |
| size int |
| head *ListNode |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| func (this *MyLinkedList) Get(index int) int { |
| |
| if index < 0 || index >= this.size { |
| return -1 |
| } |
| |
| |
| prev := this.head |
| for i:=0;i<index;i++ { |
| prev = prev.Next |
| } |
| return prev.Next.Val |
| } |
| |
| func (this *MyLinkedList) AddAtIndex(index int, val int) { |
| |
| if index < 0 || index > this.size { |
| return |
| } |
| |
| prev := this.head |
| for i:=0;i<index;i++ { |
| prev = prev.Next |
| } |
| |
| node := &ListNode{Val: val} |
| |
| node.Next = prev.Next |
| |
| prev.Next = node |
| |
| |
| this.size++ |
| } |
| |
| func (this *MyLinkedList) AddAtHead(val int) { |
| |
| this.AddAtIndex(0, val) |
| } |
| |
| func (this *MyLinkedList) AddAtTail(val int) { |
| |
| this.AddAtIndex(this.size, val) |
| } |
| |
| func (this *MyLinkedList) DeleteAtIndex(index int) { |
| |
| if index < 0 || index >= this.size { |
| return |
| } |
| |
| prev := this.head |
| for i:=0;i<index;i++ { |
| prev = prev.Next |
| } |
| |
| prev.Next = prev.Next.Next |
| |
| |
| this.size-- |
| } |
双向链表
| |
| package main |
| |
| import "fmt" |
| |
| func main() { |
| head := ConstrutorD() |
| head.AddAtHead(2) |
| head.AddAtHead(5) |
| head.AddAtHead(7) |
| head.AddAtIndex(3,4) |
| head.AddAtTail(100) |
| head.DeleteAtIndex(3) |
| fmt.Println(head.Get(0)) |
| fmt.Println(head.Get(1)) |
| fmt.Println(head.Get(2)) |
| fmt.Println(head.Get(3)) |
| } |
| |
| |
| type Node struct { |
| Val int |
| Prev, Next *Node |
| } |
| |
| type MyLinkedListD struct { |
| size int |
| head, tail *Node |
| } |
| |
| |
| func ConstrutorD() MyLinkedListD { |
| return MyLinkedListD{} |
| } |
| |
| func (this *MyLinkedListD) Get(index int) int { |
| |
| if this.size == 0 || index < 0 || index >= this.size { |
| return -1 |
| } |
| |
| if index == 0 { |
| return this.head.Val |
| } |
| if index == this.size - 1 { |
| return this.tail.Val |
| } |
| |
| cur := this.head |
| count := 0 |
| for cur != nil { |
| if count == index { |
| break |
| } |
| count++ |
| cur = cur.Next |
| } |
| return cur.Val |
| } |
| |
| func (this *MyLinkedListD) AddAtIndex(index int, val int) { |
| |
| if index > this.size { |
| return |
| } |
| |
| if index <= 0 { |
| this.AddAtHead(val) |
| return |
| } |
| |
| if index == this.size { |
| this.AddAtTail(val) |
| return |
| } |
| |
| cur := this.head |
| count := 0 |
| for cur != nil && count < index { |
| count++ |
| cur = cur.Next |
| } |
| |
| |
| node := &Node{Val: val, Next: cur, Prev: cur.Prev} |
| cur.Prev.Next = node |
| cur.Prev = node |
| |
| this.size++ |
| } |
| |
| func (this *MyLinkedListD) AddAtHead(val int) { |
| |
| node := &Node{Val: val} |
| if this.head != nil { |
| node.Next = this.head |
| this.head.Prev = node |
| this.head = node |
| } else { |
| this.head = node |
| this.tail = this.head |
| } |
| |
| this.size++ |
| } |
| |
| func (this *MyLinkedListD) AddAtTail(val int) { |
| |
| node := &Node{Val: val} |
| if this.tail != nil { |
| node.Prev = this.tail |
| this.tail.Next = node |
| this.tail = node |
| } else { |
| this.tail = node |
| this.head = this.tail |
| } |
| |
| this.size++ |
| } |
| |
| func (this *MyLinkedListD) DeleteAtIndex(index int) { |
| |
| if this.size == 0 || index < 0 || index >= this.size { |
| return |
| } |
| if index == 0 { |
| |
| this.head = this.head.Next |
| } else if index == this.size - 1 { |
| |
| this.tail = this.tail.Prev |
| } else { |
| |
| cur := this.head |
| count := 0 |
| for cur != nil && count < index { |
| count++ |
| cur = cur.Next |
| } |
| cur.Next.Prev = cur.Prev |
| cur.Prev.Next = cur.Next |
| } |
| this.size-- |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下