今天开始就进入链表的章节了,链表是一种线性表,实现链表的方式多用结构体,然后开辟动态内存。算法比赛中则一般用静态链表也就是数组模拟链表的操作,少去了类似C++里面类似new函数的操作,运行效率更快。
203.移除链表元素
移除链表元素主要有两种常用的方法,一种是在原链表进行删减,一种是利用虚拟头结点。
第一种在原链表进行删减,需要区分头节点和一般节点
如果是头节点,我们则是将这个头结点后移
while(head != NULL && head->val == val){
head = head->next;
}
如果是一般节点,我们则是直接让上一个节点的指针指要删除节点的下一个节点。
auto cur = head;
while(cur != NULL && cur->next != NULL){
if(cur->next->val == val){
cur->next = cur->next->next;
}else{
cur = cur->next;
}
}
完整代码如下:
/**
* 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 != NULL && head->val == val){
head = head->next;
}
auto cur = head;
while(cur != NULL && cur->next != NULL){
if(cur->next->val == val){
cur->next = cur->next->next;
}else{
cur = cur->next;
}
}
return head;
}
};
第二种是使用虚拟头节点,使用虚拟头节点的好处是统一了增删操作,不用再区分,头结点和非头结点。
原理跟在原链表删除一般节点的方法一样,就是设置了一个虚拟头节点方便操作。
/**
* 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) {
auto dummy_head = new ListNode(0);
dummy_head->next = head;
auto cur = dummy_head;
while(cur->next !=NULL){
if(cur->next->val == val){
cur->next = cur->next->next;
}else{
cur = cur->next;
}
}
//注意此时头节点位置
head = dummy_head->next;
return head;
}
};
707.设计链表
题目链接707. 设计链表
这道题就是链表的常规操作了,都是用虚拟头结点的方式实现的,然后注意头结点是算作第0个节点,然后遍历链表的时候要用cur做一个临时指针以免将头节点改变
class MyLinkedList {
public:
struct LinkedNode{
int val;
LinkedNode* next;
//构造函数
LinkedNode(int val):val(val),next(nullptr){}
};
LinkedNode*dummyhead;
int size;
MyLinkedList() {
dummyhead= new LinkedNode(0);
size=0;
}
int get(int index) {
if (index > (size - 1) || index < 0) {
return -1;
}
auto cur = dummyhead->next;
while(index--){
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkedNode*newnode = new LinkedNode(val);
//注意先接后面的再接前面的,这是经常犯错的地方
newnode->next = dummyhead->next;
dummyhead->next = newnode;
size++;
}
void addAtTail(int val) {
LinkedNode*newnode = new LinkedNode(val);
auto cur =dummyhead;
while(cur->next !=nullptr){
cur = cur->next;
}
cur->next = newnode;
size++;
}
void addAtIndex(int index, int val) {
if (index > size) {
return;
}
LinkedNode*newnode = new LinkedNode(val);
auto cur = dummyhead;
while(index--){
cur = cur->next;
}
newnode->next = cur->next;
cur->next = newnode;
size++;
}
void deleteAtIndex(int index) {
if (index >= size || index < 0) {
return;
}
auto cur = dummyhead;
while(index--){
cur = cur->next;
}
cur->next = cur->next->next;
size--;
}
};
/**
* 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);
*/
206.反转链表
题目链接206. 反转链表
翻转链表常用的就是双指针写法,让cur和pre指针依次往后遍历同时让cur的方向指向pre实现翻转链表,要注意的是每条赋值语句的顺序
/**
* 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 = NULL;
while(cur){
//注意语句顺序
ListNode* temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
当然,也可以用递归的写法,显得厉害一点点,其实逻辑是一样的,只是代码简短一点,也难懂一点
/**
* 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 {
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == NULL)return pre;
auto temp = cur->next;
cur->next = pre;
return reverse(cur,temp);
}
public:
ListNode* reverseList(ListNode* head) {
return reverse(NULL,head);
}
};