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

浙公网安备 33010602011771号