菜鸟笔记
提升您的技术认知

单链表的创建(有头结点和无头结点)

前言

在学链表的时候,对链表创建的过程一知半解。目前现在刷题的阶段,发现这部分很重要,所以这次完全解决这个知识点。

 

1 带头结点的链表

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

 

 1.1 头插法

头插法的思想如下图:

伪代码实现:

(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 尾插法

尾插法的思想如下图:

伪代码实现:

(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个结点的链表,链表的数据域为整数类型,取随机整数。链表结构如下图:

 

2.1 头插法

头插法思路如下:

 

伪代码实现:

(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 尾插法

尾插法思路如下:

伪代码实现:

(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;
}