当前位置:网站首页>Common linked list questions and their go implementation
Common linked list questions and their go implementation
2022-07-19 01:02:00 【InfoQ】
Delete duplicates in the linked list
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
}
}
}List reversal
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
}Linked list comparison
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 recursively compared . Besides , If we reach the end of the list and both lists are empty . Then the two lists are equal , Therefore return true
- Lists are recursively compared . If any one in the list is empty or the value of the corresponding node is not equal , This function returns false.
- Recursively call the comparison list function of the next node of the current node .
边栏推荐
- 12 张图,带你彻底理解 ZGC垃圾收集器!
- [Clickhouse] calculation of weeks
- @ConditionalOnMissingBean 如何實現覆蓋第三方組件中的 Bean
- Briefly talk about two interview questions encountered recently
- 面试官:怎么不用定时任务实现关闭订单?
- 12. Integer to Roman numerals ●●
- 软核微处理器
- “husky install“ command already exists in prepare script, skipping./Users/982471938qq.com/Desktop
- Do you want to switch to software testing? A comprehensive analysis
- TCP和UDP的区别
猜你喜欢

盛夏防火患!广东开展夏季消防安全检查

A brief record of UML class diagram

How to design interface test cases? (the interface test case template is sent at the end of the document)

Mysql—锁:全面理解

(手工)【sqli-labs62-65】限制注入次数:盲注

12. Integer to Roman numerals ●●

抖音抓包通杀版-version21.5

Hcia-r & s self use notes (6) (network layer) ICMP, IP protocol foundation and fragmentation

64 bit interrupt assembly cannot be used

Press and hold day 27: Service
随机推荐
1764. 通过连接另一个数组的子数组得到一个数组 ●●
Mon anniversaire de création
Is it safe to open an account with baoguoxin securities? Can I drive it?
从编译器对指令集的要求看API设计原则
【著色器實現Wave效果_Shader效果第一篇】
Greenplum common
【着色器实现Wave效果_Shader效果第一篇】
Greenplum 6.x client connection
基于yoloV5的.pt模型转换为.engine模型,并通过Deepstream加速,可多相机实时传输,采用nvidia的Xavier板子(精一)
Go BitSet of one library per day
支付宝国信证券开户安全吗?可以开吗?
Mysql—锁:全面理解
JVM 内存布局详解,图文并茂,写得太好了!
软核微处理器
[paper notes] - feature visualization - zfnet - 2014-eccv
Statement on the standardized use of personal sensitive information in the app privacy policy text
按住 Day 27 :服务
2. Research on stm32f4 USB protocol - SD card analog USB stick
Vite3.0 release
交换机与路由器技术:链路聚合、生成树协议STP和生成树协议配置