go中单链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | package main import ( "fmt" ) type ListNode struct { Val int Next *ListNode } type List struct { headNode *ListNode //头节点 } func main() { //res := letcode.Divide(434, 423) //fmt.Println(res) headNode := &ListNode{} listData := headNode Insert(1, listData, headNode) Insert(2, listData, headNode) Insert(4, listData, headNode) res := Find(2, listData) fmt.Println(res) //listData2 := headNode // //Insert(4, listData2, headNode) //Insert(3, listData2, headNode) //Insert(2, listData2, headNode) //PrintList(listData) //res := MergeTwoLists(listData, listData2) //fmt.Println(res) } //删除节点 func Delete(value int , list *ListNode) { pPrev := FindPrevious(value, list) _ = pPrev p := Find(value, list) pPrev = p.Next p = nil } // FindPrevious ... func FindPrevious(value int , list *ListNode) *ListNode { p := list for p.Next != nil && p.Next.Val != value { p = p.Next } return p } // 找出链表里面的(不安全) func Find(value int , list *ListNode) *ListNode { p := list for p.Val != value { p = p.Next } return p } // 测试是否为最后节点 ... func IsLast(list *ListNode) bool { return list.Next == nil } // 测试链表是否为空 ... func isEmpty(list *ListNode) bool { return list == nil } // 打印链表 ... func PrintList(list *ListNode) { if list.Next != nil { fmt.Println(list.Val) PrintList(list.Next) } else { fmt.Println(list.Val) } } // 合并两个有序的单链表 ... func MergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { if l1 == nil { return l2 } if l2 == nil { return l1 } var res *ListNode if l1.Val >= l2.Val { res = l2 res.Next = MergeTwoLists(l1, l2.Next) } else { res = l1 res.Next = MergeTwoLists(l1.Next, l2) } return res } // 插入节点 头插法 func Insert(value int , list *ListNode, position *ListNode) { tempCell := new (ListNode) //fmt.Println("++++", tempCell) if tempCell == nil { fmt.Println( "out of space!" ) } tempCell.Val = value tempCell.Next = position.Next position.Next = tempCell } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署