当前位置:网站首页>Common linked list problems and their Go implementation
Common linked list problems and their Go implementation
2022-07-30 18:31:00 【HUAWEI CLOUD】
Remove duplicates from linked list
Remove a duplicate value from the linked list, the linked list is ordered.In a sorted linked list, there are duplicate nodes, please delete the duplicate nodes in the linked list, the duplicate nodes are not retained, and the pointer to the head of the linked list is returned.For example, the linked list 1->2->3->3->4->4->5 is processed as 1->2->5
Go language implementation is as follows:
func(list *List) RemoveDuplicate() { curr := list.head for curr != nil { if curr.next != nil && curr.value == curr.next.value { curr.next = curr.next.next } else { curr = curr.next } }}Algorithm Analysis:
The algorithm uses only one layer of loops, the for loop is used to traverse the list.Whenever there is a node whose value is equal to the value of the next node, the current node next will point to the next of the next node.This will remove the next node from the list, the algorithm complexity is O(n)
List Reverse
Copy the contents of a linked list to another linked list in reverse order.If the original linked list contains elements in the order 1,2,3,4, the new linked list should contain elements in the order 4,3,2,1.
func(list *List) CopyListReversed() *List { var tempNode, tempNode2 * Node curr := list.head for curr != nil { tempNode2 = &Node{curr.value, tempNode} curr = curr.next tempNode = tempNode2 } ll2 := new(List) ll2.head = tempNode return ll2}Traverse the list and add the node's value to a new list.Since the list is traversed forward and each node's value is added to the other list, the resulting list is the inverse of the given list.
Linked list comparison
Compares the values of two linked lists for a given head pointer.
func (list *List) CompareList(ll *List) bool { return list.compareListUtil(list.head, ll.head)}func(list *List) compareListUtil(head1*Node, head2 *Node) bool { if head1 == nil && head2 == nil { return true } else if (head1 == nil) || (head2 == nil) || (head1.value != head2.value) { return false } else { return list.compareListUtil(head1.next, head2.next) }}Lists are compared recursively.Also, if we reach the end of the list and both lists are empty.Then the two lists are equal, so return true
Lists are compared recursively.This function will return false if any of the list is empty or if the corresponding node values are not equal.
Recursively calls the compare list function of the next node of the current node.
边栏推荐
- Application of time series database in the field of ship risk management
- Kettle(二):连接SQL Server数据库
- linux 安装mysql8.0 超详细教程(实战多次)
- 基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
- 千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践
- Recommendation | People who are kind to you, don't repay them by inviting them to eat
- Scrapy framework is introduced
- 国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
- 【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
- 第4章 控制执行流程
猜你喜欢

基于inquirer封装一个控制台文件选择器

SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)

博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显

【AGC】增长服务2-应用内消息示例

CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配

MySQL data types

One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...

6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?

Redis for infrastructure

网络基础(二)-Web服务器-简介——WampServer集成服务器软件之Apache+MySQL软件安装流程 & netstat -an之检测计算机的端口是否占用
随机推荐
自然语言处理nltk
Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
线性筛求积性函数
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
高精度加法
这玩意儿都能优化?果然是细节都在魔鬼里。
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
Confluence OGNL注入漏洞复现(CVE-2022-26134)
荐书 | 推荐好评如潮的3本数据库书籍
微博广告分布式配置中心的构建与实践(有彩蛋)
DevEco Studio3.0下载失败,提示An unknown error occurred
Kettle(二):连接SQL Server数据库
Application of time series database in the field of ship risk management
Recommendation | People who are kind to you, don't repay them by inviting them to eat
Pagoda builds PHP adaptive lazy website navigation source code measurement
A senior with 13 years of experience in software testing, summed up 5 test employment suggestions....
攻防世界web-Cat
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
Anaconda Navigator stuck on loading applications
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用