203. 移除链表元素
题目链接:https://leetcode.cn/problems/remove-linked-list-elements/description/
文章讲解:https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html
视频讲解: https://www.bilibili.com/video/BV18B4y1s7R9/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while(head != NULL && head->val == val)
{
ListNode *p = head->next;
delete head;
head = p;
}
if(head == NULL) return NULL;
ListNode *q = head;
ListNode *p = head->next;
while(p != NULL)
{
if(p->val == val)
{
q->next = p->next;
delete p;
p = q->next;
}
else
{
p = p->next;
q = q->next;
}
}
return head;
}
};
原来学数据结构的时候就喜欢这么写,所以按照自己的习惯写的。
下面是创建一个头节点的写法
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *temphead = new ListNode(0);
temphead->next = head;
ListNode *q = temphead;
ListNode *p = head;
while(p != NULL)
{
if(p->val == val)
{
q->next = p->next;
delete p;
p = q->next;
}
else
{
p = p->next;
q = q->next;
}
}
if(head == NULL) return NULL;
head = temphead->next;
delete temphead;
return head;
}
};
707. 设计链表
题目链接:https://leetcode.cn/problems/design-linked-list/description/
文章讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html
视频讲解:https://www.bilibili.com/video/BV1FU4y1X7WD/
我觉得这道题对我的难点应该就是各种条件的判断,具体实现不难,数据结构里面讲的很详细。
class MyLinkedList {
public:
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val) : val(val), next(nullptr) {}
};
MyLinkedList() {
temphead = new LinkedNode(0);
size = 0;
}
int get(int index) {
if (index > (size - 1) || index < 0) {
return -1;
}
LinkedNode *p = temphead->next;
while(index)
{
p = p->next;
index--;
}
return p->val;
}
void addAtHead(int val) {
LinkedNode *p = new LinkedNode(val);
p->next = temphead->next;
temphead->next = p;
size++;
}
void addAtTail(int val) {
LinkedNode *p = new LinkedNode(val);
LinkedNode *q = temphead;
while(q->next != nullptr)
{
q = q->next;
}
q->next = p;
size++;
}
void addAtIndex(int index, int val) {
if(index > size) return;
if(index < 0) index = 0;
LinkedNode *temp = new LinkedNode(val);
LinkedNode *q = temphead;
while(index--)
{
q = q->next;
}
temp->next = q->next;
q->next = temp;
size++;
}
void deleteAtIndex(int index) {
if (index >= size || index < 0) {
return;
}
LinkedNode *p = temphead->next;
LinkedNode *q = temphead;
while(index--)
{
q = q->next;
p = p->next;
}
q->next = p->next;
delete p;
p = q->next;
size--;
}
void printLinkedList() {
LinkedNode* cur = temphead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int size;
LinkedNode* temphead;
};
还是要注意实现的具体细节,不能太马虎。
206.反转链表
题目链接:https://leetcode.cn/problems/reverse-linked-list/description/
文章讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html
视频讲解:https://www.bilibili.com/video/BV1nB4y1i7eL/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *temphead = new ListNode(0);
ListNode *p = head;
while(p != NULL)
{
ListNode *q = p->next;
p->next = temphead->next;
temphead->next = p;
p = q;
}
head = temphead->next;
return head;
}
};
头插法还是比较简单的,递归还不太会。先留着。