go实现反转链表
作者:鹿灏楷silves 时间:2024-02-07 12:54:59
反转链表首先讨论特殊节点
如果节点在首位,则反转之后,首位节点的next值为nil。
func reverse(head *ListNode) *ListNode {
bnode := head//设置默认遍历的前节点,为head
temp := head.Next//从head.next节点开始遍历链表
flage := 0//设置一个标记,用于判断是否为第一个节点
var lnext *ListNode//用于临时保存下一个节点
for temp != nil {//遍历当
lnext = temp.Next//先将遍历节点的下一个节点进行保存
temp.Next = bnode//节点反转,将此节点的next值进行更改,更改为上一个节点
if flage == 0 {//如果是第一个节点,则flage为0
bnode.Next = nil//第一个节点翻转后的next值为nil
flage = 1//将flage值进行更改,避免之后的节点的next值被更改为nil
}
bnode = temp//将节点进行更新,前节点更改为遍历节点
temp = lnext//节点进行更新,由于之前temp.next值已经被更改,但是temp.next值被保存在变量中所以temp = lnext
}
return bnode//返回bnode节点,不能返回temp节点,temp为空,因为temp为空是跳出for循环的标志
}
补充:golang 链表逆序反转输出
链表逆序输出案例代码
//如何实现链表的逆序
package main
import "fmt"
type studentNode struct {
no int
name string
age int
next *studentNode
pre *studentNode
}
func reversePrint(head *studentNode) {
//1.创建一个辅助结点
temp := head
//2.判断这是不是一个空链表
if temp.next == nil { //说明是一个空链表
return
}
//3.让temp指向这个双向链表的最后结点
for {
if temp.next == nil {
break
}
temp = temp.next
} //循环结束则已经指向双向链表的最后结点
//4.遍历这个链表
for{
fmt.Printf("%d,%s,%d==>",temp.no,temp.name,temp.age)
temp=temp.pre//依次向前
if temp.pre==nil{//判断是不是到了链表头
break
}
}
}
func insertStudentNode(head *studentNode, newStudentNode *studentNode) {
//思路:
//1.先找到该链表的最后这个结点
//2.创建一个辅助结点
temp := head
for {
if temp.next == nil { //表示找到最后
break
}
temp = temp.next //让temp不断的指向下一个结点
}
//3.将newStudentNode加入到链表的最后
temp.next = newStudentNode
newStudentNode.pre=temp
}
//正序打印这个双向链表
func originalList(head *studentNode) {
//1.创建一个辅助结点
temp:=head
//2.先判断这个链表是否是空链表
if temp.next==nil{
fmt.Println("这是一个空链表")
return
}
//3.遍历这个链表
for{
fmt.Printf("%d,%s,%d==>",temp.next.no,temp.next.name,temp.next.age)
temp=temp.next
if temp.next == nil{
break
}
}
}
//创建一个双向链表
func createlink()*studentNode {
//1.先创建一个头结点
head := &studentNode{}
//2.创建一个新的studentNode
stuLisa := &studentNode{
no: 1,
name: "Lisa",
age: 24,
}
stuBob := &studentNode{
no: 2,
name: "Bob",
age: 25,
}
stuNick := &studentNode{
no: 3,
name: "Nick",
age: 27,
}
stuMark := &studentNode{
no: 4,
name: "Mark",
age: 29,
}
//3.加入结点
insertStudentNode(head, stuLisa)
insertStudentNode(head, stuBob)
insertStudentNode(head, stuNick)
insertStudentNode(head, stuMark)
return head
}
//逆序打印这个双向链表
func main() {
head :=createlink()
fmt.Println("正序打印")
originalList(head)
fmt.Println()
fmt.Println("逆序打印")
reversePrint(head)
}
结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/Xiang_lhh/article/details/110221362
标签:go,反转,链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
超全面python常见报错以及解决方案梳理必收藏
2022-02-14 16:33:17
Python 快速验证代理IP是否有效的方法实现
2022-08-12 07:22:14
python中ImageTk.PhotoImage()不显示图片却不报错问题解决
2023-08-26 18:12:06
![](https://img.aspxhome.com/file/2023/9/69589_0s.png)
详细解析Webpack是怎么运行的
2022-09-07 04:00:50
![](https://img.aspxhome.com/file/2023/1/131841_0s.png)
PyCharm专业最新版2019.1安装步骤(含激活码)
2022-10-21 19:56:06
![](https://img.aspxhome.com/file/2023/7/89217_0s.png)
Python实现智能贪吃蛇游戏的示例代码
2023-01-28 13:39:36
![](https://img.aspxhome.com/file/2023/1/100861_0s.jpg)
pygame实现非图片按钮效果
2021-08-30 23:21:14
![](https://img.aspxhome.com/file/2023/9/95929_0s.gif)
在 Pycharm 安装使用black的方法详解
2023-11-29 12:44:59
![](https://img.aspxhome.com/file/2023/1/133231_0s.png)
python计算机视觉OpenCV库实现实时摄像头人脸检测示例
2022-05-17 22:41:50
![](https://img.aspxhome.com/file/2023/5/105305_0s.png)
Python 编程速成(推荐)
2021-04-03 04:30:10
使用python BeautifulSoup库抓取58手机维修信息
2022-08-10 01:55:20
SQL Server保障数据一致性的法宝
2008-12-26 15:21:00
Bootstrap 模态对话框只加载一次 remote 数据的完美解决办法
2023-08-06 15:58:18
![](https://img.aspxhome.com/file/2023/0/55970_0s.png)
Flash的Fallback Content等
2010-04-01 12:18:00
python爬取亚马逊书籍信息代码分享
2021-03-25 09:38:14
Mysql Explain命令的使用与分析
2024-01-29 08:39:04
![](https://img.aspxhome.com/file/2023/0/122910_0s.png)
linux环境下安装python虚拟环境及注意事项
2023-12-09 08:02:47
Python中使用多进程来实现并行处理的方法小结
2023-10-23 14:21:38
Python利用wxPython制作股票价格查询工具
2021-11-25 19:09:52
![](https://img.aspxhome.com/file/2023/2/109452_0s.png)
PHP chunk_split()函数讲解
2023-06-09 13:54:44
![](https://img.aspxhome.com/file/2023/5/55395_0s.png)