Golang实现反转单链表
source link: https://www.fdevops.com/2021/05/20/go-30766
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
实现原理介绍
首先先认识一下链表这个数据结构:
链表节点中有两个元素:
Next指向下一个节点
那么这个问题其实就是把指针指向前一个节点
位置调换次数precurwhole0nil1->2->3->4->51->2->3->4->511->nil2->-3>->4->52->3->4->5->1->nil22->1->nil3->4->53->4->5->2->1->nil33->2->1->nil4->54->5->3->2->1->nil44->3->2->1->nil55->4->3->2->1->nil可以看出来
- pre是cur的最前面那位(pre = cur)
- cur就是当前位的后面链表元素(cur = cur.Next)
- cur.Next肯定是接pre(cur.Next = pre)
https://www.cnblogs.com/TimLiuDream/p/9932494.html
本文为原创文章,未经授权禁止转载本站文章。
原文出处:兰玉磊的个人博客
原文链接:https://www.fdevops.com/2021/05/20/go-30766
版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可。
Recommend
-
30
转载请注明出处: 数据结构——Golang实现单链表 Golang
-
34
-
26
单链表定义 此处将 Node 和 List 分开定义,方便理解,官方的方法是把两个结构合并, 戳:https://golang.org/src/container/list/list.go?s=2063:2093#L14
-
17
数据结构与算法的JavaScript实现及应用 – 单链表 为什么要写这个系列? 程序=数据结构+算法。 我对这句话的理解是构建程序等于解决问题。解决问题需要工具和方法,数据结构是工具,算法则是方法。 最近辞职了赋闲在...
-
13
问题一、反转单链表题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。 链表结点定义如下,这里使用的是Java描述: public class ListNode { int val; ListNode next; List...
-
4
golang反转单链表 TangYiMo · 大约3小时之前 · 54 次点击 · 预计阅读时间 3 分钟 · 大约8小...
-
4
#yyds干货盘点# 单链表实现栈 原创 wx61dbfa0f42bf6 2022-01-17 11:18:41...
-
4
单链表(java)——简单实现 原创 阿炳不会扣篮 2022-01-17 10:56:48...
-
6
递归魔法:反转单链表
-
7
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK