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

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

参考博客:链表头结点和不带头结点的区别单链表的创建(有头结点和无头结点)

一、概念辨析

线性表的插入删除需要移动大量的元素,因此引入链表(本文讨论单链表)的概念,链表元素之间通过“链”来链接,因此插入和删除时不需要大量的移动元素,而只需要改变“链”的关系即可。

头指针:通常使用“头指针”来标识一个链表,如单链表L,头指针为NULL时表示一个空链表。链表非空时,头指针指向的是第一个结点的存储位置。

头结点:在单链表的第一个结点之前附加一个结点,称为头结点。头结点的Data域可以不设任何信息,也可以记录表长等相关信息。若链表是带有头结点的,则头指针指向头结点的存储位置。

[注意]无论是否有头结点,头指针始终指向链表的第一个结点。如果有头结点,头指针就指向头结点。

在这里插入图片描述

二、引入头结点的优势

优势1:第1个位置的插入、删除更加方便,带来操作上的统一

例如:head为头指针,x待插入(后插方式)的新结点,p为指向任意结点的指针。

对于带头结点的插入到第一个位置的代码:

//p = head;
x->next = head->next;
head->next = x;
插入其他结点

x->next = p->next;
p->next = x;

若令p=head,则带有头结点的链表,可以实现代码复用,减少分支。

对于不带头结点的插入到第一个位置的代码:

x->next = head;
head = x;
插入其他结点

x->next = p->next;
p->next = x;
因此,不带头结点的链表,插入第一个结点时,需要特殊处理,删除操作类似。

优势2:统一空表和非空表的处理

若使用头结点,无论表是否为空,头指针都指向头结点,也就是*LNode类型,对于空表和非空表的操作是一致的。

若不使用头结点,当表非空时,头指针指向第1个结点的地址,即*LNode类型,但是对于空表,头指针指向的是NULL,此时空表和非空表的操作是不一致的。

所以单链表一般为带头结点的单链表

三、带头结点和不带头结点的头插法和尾插法

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

img

1.1 头插法
头插法的思想如下图:

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;
}
  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值