链表知识
类型
单链表
双链表
循环链表
存储方式
数组是在内存中是连续分布的,但是链表在内存中可不是连续分布的。
链表是通过指针域的指针链接在内存中各个节点。
所以链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
203. 移除链表元素
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
思路:可以理解链表的增删过程但是对于代码还是不熟悉,看一遍代码才可以完整的写出来。
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
//删除头结点
while(head!=nullptr&&head->val==val){
ListNode* tmp=head;
head=head->next; //指针移动
delete tmp;
}
//删除非头结点
ListNode* pre=head; //记录头结点
while(head!=nullptr&&head->next!=nullptr){
if(head->next->val==val){
ListNode* t=head->next;
head->next=head->next->next; //指针移动
delete t;
}
else{
head=head->next; //指针移动
}
}
return pre;
}
};
!注意链表的定义
!注意标记头结点
使用Java没有指针,链表定义方式为:
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
重点掌握虚拟头结点法:
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead=new ListNode(0);
dummyHead->next=head;
ListNode* cur=dummyHead;
while(cur->next!=nullptr){
if(cur->next->val==val){
ListNode* tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
}
else{
cur=cur->next;
}
}
head=dummyHead->next;
delete dummyHead;
return head;
}
};
使用C++实现指针时,需要手动释放指针内存。
使用java/Python系统自动管理内存空间。
707. 设计链表
使用虚拟头节点基本可以写出,但是一开始还是一直报错。
一开始的版本:
class MyLinkedList {
public:
struct ListNode{
int val;
ListNode* next;
ListNode(int val):val(val),next(nullptr){};
};
MyLinkedList() {
dummyHead=new ListNode(0);
size=0;
}
int get(int index) {
if(index>(size-1)||index<0){
return -1;
}
ListNode* cur=dummyHead->next;
for(int i=0;i<index;i++){
cur=cur->next;
}
return cur->val;
}
void addAtHead(int val) {
ListNode* newnode=new ListNode(val);
newnode->next=dummyHead->next;
dummyHead->next=newnode;
size++;
}
void addAtTail(int val) {
ListNode* node=new ListNode(val);
ListNode* cur=dummyHead;
for(int i=0;i<size-1;i++){ //遍历到链表最后一位
cur=cur->next;
}
cur->next=node; //将链表最后一位指向新节点
size++;
}
void addAtIndex(int index, int val) {
if(index>size){
return ;
}
ListNode* node=new ListNode(val);
if(index<0) index=0;
ListNode* cur=dummyHead;
for(int i=0;i<index;i++){
cur=cur->next;
}
node->next=cur->next;
cur->next=node;
size++;
}
void deleteAtIndex(int index) {
if(index<size-1){
ListNode* cur=dummyHead;
for(int i=0;i<index;i++){
cur=cur->next;
}
ListNode* tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
size--;
}
}
private:
int size;
ListNode* dummyHead;
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
报错:runtime error: member access within null pointer of type 'MyLinkedList::ListNode' (solution.cpp)
参考文章的分析LeetCodeBug-member access within null pointer of type 'struct ListNode'_zy2317878的博客-CSDN博客
这个问题主要是由于测试系统不知道当前的结点与结点的下一个结点是否为NULL,而自己有没有声明,所以系统不确定,于是就报错。这也说明了链表中的结点使用的时候比其他数据结构要严格一些,一定要时刻保证使用的结点不为NULL,为NULL要做出相应处理。
class MyLinkedList {
public:
struct ListNode{
int val;
ListNode* next;
ListNode(int val):val(val),next(nullptr){};
};
MyLinkedList() {
dummyHead=new ListNode(0);
size=0;
}
int get(int index) {
if(index>(size-1)||index<0){
return -1;
}
ListNode* cur=dummyHead->next;
while(index--){
cur=cur->next;
}
return cur->val;
}
void addAtHead(int val) {
ListNode* newnode=new ListNode(val);
newnode->next=dummyHead->next;
dummyHead->next=newnode;
size++;
}
void addAtTail(int val) {
ListNode* node=new ListNode(val);
ListNode* cur=dummyHead;
// for(int i=0;i<size-1;i++){ //遍历到链表最后一位
// cur=cur->next;
// } //空指针报错来源
while(cur->next!=nullptr){
cur=cur->next;
}
cur->next=node; //将链表最后一位指向新节点
size++;
}
void addAtIndex(int index, int val) {
if(index>size){
return ;
}
if(index<0) index=0;
ListNode* node=new ListNode(val);
ListNode* cur=dummyHead;
while(index--){
cur=cur->next;
}
node->next=cur->next;
cur->next=node;
size++;
}
void deleteAtIndex(int index) {
// if(index<size&&index>0){
// ListNode* cur=dummyHead;
// while(index--){
// cur=cur->next;
// }
// ListNode* tmp=cur->next;
// cur->next=cur->next->next;
// delete tmp;
// size--;
// } //结果错误报错来源
if(index>=size||index<0) return ;
ListNode* cur=dummyHead;
while(index--){
cur=cur->next;
}
ListNode* tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
size--;
}
private:
int size;
ListNode* dummyHead;
};
206. 反转链表
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
示例 1:
输入:head = [1,2,3,4,5] 输出:[5,4,3,2,1]
基本有思路,形成代码比较慢,最后还是报错。
参考下图编写直接ac,采用双指针,注意初始化的巧妙之处
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* cur=head;
ListNode* pre=nullptr;
ListNode* tmp;
while(cur!=nullptr){
tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
return pre;
}
};
//自己写的版本一直超出时间限制,还没看懂是什么问题
// class Solution {
// public:
// ListNode* reverseList(ListNode* head) {
// ListNode* pre=head;
// ListNode* post;
// while(head!=nullptr){
// post=head->next;
// } //注意空指针问题
// ListNode* tmp;
// pre->next=nullptr;
// while(post!=nullptr){
// tmp=pre;
// pre=post;
// post=post->next;
// pre->next=tmp;
// }
// return pre;
// }
// };
时间复杂度O(n)
空间复杂度O(1)