代码随想录算法训练营第三天 | LeetCode203移除链表元素、LeetCode707设计链表、LeetCode206反转链表
时长:大约3小时
203. Remove Linked List Elements
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head .
Example 1:
![](https://i-blog.csdnimg.cn/blog_migrate/fe33c5a85318e639bf0b0cccc3a86807.jpeg)
Example 2:
Example 3:
Constraints:
The number of nodes in the list is in the range [0, 10<sup>4</sup>].
1 <= Node.val <= 50
0 <= val <= 50
解题思路:
主要注意处理头部删除和中间删除,可以添加dummyHead来统一这两者
需要注意的要点:
添加了dummyHead
要返回dummyHead->next
从dummyHead开始循环,处理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) {
if (head == NULL) {
return head;
}
ListNode* pre = NULL;
ListNode* tmp = head;
while(tmp != NULL) {
if(head->val == val && pre == NULL) {
ListNode* garbage = tmp;
tmp = tmp->next;
head = head->next;
delete garbage;
}
else if(tmp->val == val && pre != NULL) {
ListNode* garbage = tmp;
pre->next = tmp->next;
tmp = tmp->next;
delete garbage;
}
else {
pre = tmp;
tmp = tmp->next;
}
}
return head;
}
};
// carl
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyNode = new ListNode(0);
dummyNode->next = head;
ListNode* cur = dummyNode;
while(cur != NULL and cur->next != NULL) {
if(cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
}
else {
cur = cur->next;
}
}
head = dummyNode->next;
delete dummyNode;
return head;
}
};
707. Design Linked List
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed .
Implement the MyLinkedList class:
MyLinkedList() Initializes the MyLinkedList object.
int get(int index) Get the value of the index<sup>th</sup> node in the linked list. If the index is invalid, return -1.
void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
void addAtTail(int val) Append a node of value val as the last element of the linked list.
void addAtIndex(int index, int val) Add a node of value val before the index<sup>th</sup> node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted .
void deleteAtIndex(int index) Delete the index<sup>th</sup> node in the linked list, if the index is valid.
Example 1:
Constraints:
0 <= index, val <= 1000
Please do not use the built-in LinkedList library.
At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.
解题思路:
按步骤实现每一部分的功能即可
可以采用dummyHead来统一处理元素添加
中间添加
头部添加
删除是对cur->next处理,从dummyHead开始循环
添加也是对cur->next处理,从dummyHead开始循环
可以记录元素数目对index进行判断
需要注意的要点:
addIndex的index可以等于当前的链表,意味着在尾部添加
//我的实现
class MyLinkedList {
public:
int val;
MyLinkedList* next;
MyLinkedList() {
this->val = -1;
this->next = NULL;
}
int get(int index) {
MyLinkedList* tmp = this->next;
int count = 0;
while(tmp != NULL && count < index) {
tmp = tmp->next;
count ++;
}
if(tmp == NULL) {
return -1;
}
else {
return tmp->val;
}
}
void addAtHead(int val) {
MyLinkedList* node = new MyLinkedList;
node->val = val;
node->next = this->next;
this->next = node;
}
void addAtTail(int val) {
MyLinkedList* node = this;
cout << this->next << endl;
while(node->next != NULL) {
node = node->next;
}
MyLinkedList* newNode = new MyLinkedList;
newNode->val = val;
newNode->next = NULL;
node->next = newNode;
}
void addAtIndex(int index, int val) {
MyLinkedList* prev = this;
MyLinkedList* cur = this->next;
int count = 0;
while(cur != NULL && count < index) {
prev = cur;
cur = cur->next;
count ++;
}
if(count == index) {
MyLinkedList* newNode = new MyLinkedList;
newNode->val = val;
newNode->next = NULL;
if (cur == NULL) {
prev->next = newNode;
}
else {
prev->next = newNode;
newNode->next = cur;
}
}
}
void deleteAtIndex(int index) {
MyLinkedList* prev = this;
MyLinkedList* cur = this->next;
int count = 0;
while(cur != NULL && count < index) {
prev = cur;
cur = cur->next;
count ++;
}
if(count == index) {
if (cur != NULL) {
prev->next = cur->next;
}
}
}
};
/**
* 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);
*/
//carl实现
class MyLinkedList {
public:
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(NULL) {}
};
MyLinkedList() {
_dummyHead = new LinkedNode(0);
_size = 0;
}
int get(int index) {
if(index >= _size || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;
while(index --) {
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkedNode* cur = new LinkedNode(val);
cur->next = _dummyHead->next;
_dummyHead->next = cur;
_size ++;
}
void addAtTail(int val) {
LinkedNode* cur = _dummyHead;
while(cur->next != NULL) {
cur = cur->next;
}
LinkedNode* newNode = new LinkedNode(val);
cur->next = newNode;
_size ++;
}
void addAtIndex(int index, int val) {
if(index < 0 || index > _size) {
return;
}
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index --) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size ++;
}
void deleteAtIndex(int index) {
if(index < 0 || index >= _size) {
return;
}
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size --;
}
private:
int _size;
LinkedNode* _dummyHead;
};
206. Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list .
Example 1:
![](https://i-blog.csdnimg.cn/blog_migrate/92168feffa7541cdb93eced8f2227e26.jpeg)
Example 2:
![](https://i-blog.csdnimg.cn/blog_migrate/e8f44e3ec56bfdd01f04f1c83f97d88a.jpeg)
Example 3:
Constraints:
The number of nodes in the list is the range [0, 5000].
-5000 <= Node.val <= 5000
Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?
解题思路:
需要记录三个变量:
prev
cur
next
把cur->next转为prev,然后进到next,循环或迭代处理。
需要注意的要点:
需要记录next,不然没办法进入下一个节点处理
返回的时候由于cur为NULL,返回prev即可
/**
* 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) {
if (head == NULL) {
return NULL;
}
ListNode* prev = NULL;
ListNode* cur = head;
ListNode* next;
while(cur != NULL) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}
};
// 迭代
class Solution {
public:
ListNode* reverseList(ListNode* head) {
return reverse(head, NULL);
}
ListNode* reverse(ListNode* cur, ListNode* prev) {
if(cur == NULL) {
return prev;
}
ListNode* next = cur->next;
cur->next = prev;
return reverse(next, cur);
}
};
声明:
文章中LeetCode的题目来自LeetCode官网:https://leetcode.cn/problems