【LeetCode刷题日记】链表带头结点和不带头结点的区别

头插法的思想如下图:

img

伪代码实现:
(1)创建一个头结点,ListNode *head = new ListNode(10) ; //头结点数据域保存结点的个数
head -> next = nullptr;
(2)插入结点1,LIstNode *s = new ListNode(rand()); // 创建结点1
s - > next = head -> next;
head -> next = s;
(3)插入结点2,LIstNode *s = new ListNode(rand()); // 创建结点2
s - > next = head -> next; // 这里 head - > next 其实就是结点1
head -> next = s;
(4)重复上述流程,创建完成

1.2 头插法代码实现

struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr){
}
}

// 头插法
ListNode* CreateList(int length){
if (length < 1)
return nullptr;
ListNode *head = new ListNode(length);
head -> next = nullptr;
int k = 1;
ListNode *s = nullptr;
srand(unsigned(time(0)));
while (k <= length){
s = new ListNode(rand());
s -> next = head -> next;
head -> next = s;
k++;
}
return head;
}

1.3 尾插法
尾插法的思想如下图:

img

伪代码实现:
(1)创建一个头结点,ListNode *head = new ListNode(10) ; //头结点数据域保存结点的个数
ListNode *s = head;
(2)插入结点1,LIstNode *r = new ListNode(rand()); // 创建结点1
s -> next = r;
s = r;
(3)插入结点2,LIstNode *r = new ListNode(rand()); // 创建结点2
s - > next = r;
s = r;
(4)重复上述流程,最后时,让s -> next = nullptr,返回head。

1.4 尾插法代码实现

struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr){
}
}

// 尾差法
ListNode* CreateList(int length){
if (length < 1)
return nullptr;
ListNode *head = new ListNode(length);
ListNode *s = head;
int k = 1;
ListNode *r = nullptr;
srand(unsigned(time(0)));
while (k <= length){
r = new ListNode(rand());
s -> next = r;
s = r;
k++;
}
s -> next = nullptr;
return head;
}

2 无头结点的链表
为了方便,创建带有10个结点的链表,链表的数据域为整数类型,取随机整数。链表结构如下图:

img

2.1 头插法
头插法思路如下:

img

伪代码实现:
(1)创建结点1,ListNode *head = new ListNode(10) ; //头结点数据域保存结点的个数
head -> next = nullptr;
ListNode *s = nullptr;
(2)插入结点2,LIstNode *s = new ListNode(rand()); // 创建结点1
s -> next = head;
head = s;
(3)插入结点3,LIstNode *s = new ListNode(rand()); // 创建结点2
s -> next = head;
head = s;
(4)重复上述流程,创建完成

2.2 头插法代码实现

struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr){
}
}

// 头插法
ListNode* CreateList(int length){
if (length < 1)
return nullptr;
srand(unsigned(time(0)));
ListNode *head = new ListNode(rand());
head -> next = nullptr;
ListNode *s = nullptr;
int k = 1;
while (k <= length - 1){
s = new ListNode(rand());
s -> next = head;
head = s;
k++
}
return head;
}

2.3 尾插法
尾插法思路如下:

img

伪代码实现:
(1)创建结点1,ListNode *head = new ListNode(10) ; //头结点数据域保存结点的个数
ListNode *s = head;
(2)插入结点2,LIstNode *r = new ListNode(rand()); // 创建结点1
s -> next = r;
s = r;
(3)插入结点3,LIstNode *r = new ListNode(rand()); // 创建结点2
s -> next = r;
s = r;
(4)重复上述流程,最后时,让s -> next = nullptr,返回head。

2.4 尾插法代码实现

struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr){
}
}

// 头插法
ListNode* CreateList(int length){
if (length < 1)
return nullptr;

srand(unsigned(time(0)));
ListNode *head = new ListNode(rand());
ListNode *s = head, *r = nullptr;
int k = 1;
while (k <= length - 1){
r = new ListNode(rand());
s -> next = r;
s = r;
k++
}
s -> next = nullptr;
return head;
}

// 将头结点放在循环里面
ListNode* CreateList(int length){
if (length < 1)
return nullptr;

srand(unsigned(time(0)));
ListNode *head = nullptr, *s = nullptr, *r = nullptr;
int k = 1;
while (k <= length){
r = new ListNode(rand());
if (head == nullptr)
head = r;
else
s -> next = r;
s = r;
k++
}
s -> next = nullptr;
return head;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Go语言工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Go语言全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-8w1jaYAM-1712925628520)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值