Linked List 链表

链表

一种储存无序的线性数据结构

优点:易插入,在内存允许的情况下,可以随意改变其长度

缺点:查找慢

一、链表的创建

1、By C
typedef struct ListNode{
    int val;
    struct ListNode* next;
}ListNode;

2、By C++
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

二、链表相关操作

1、链表的遍历
// By C++
void Order(ListNode* head){
    while(head){
        //链表节点操作位
        head=head->next;
    }
}

2、链表反转
// By C++
//  无表头反转
//  1 -> 2 -> 3 -> 4 -> 5 -> NULL;
//  5 -> 4 -> 3 -> 2 -> 1 -> NULL;
// Head                fix   temp
ListNode* ReverseLink(ListNode* Head){
    ListNode* fix = Head;
    ListNode* temp = Head->next;
    while(temp){
        fix->next = temp->next;
        temp->next = Head;
        Head = temp;
        temp = fix->next;
    }
    return Head;
}

posted @ 2022-08-30 20:42  捏鼠  阅读(53)  评论(0)    收藏  举报