203.移除链表元素
day3!最近事情比较多,再加上学的是真的吃力,尤其是707设计链表,写了好几遍。之前只水过课自己没有用过,勉强跟着看懂了然后写了一遍。抓紧补补补!加油。
删除链表中等于给定值 val 的所有节点。
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
// 删除头结点
while (head != NULL && head->val == val) { // 注意这里不是if
ListNode* tmp = head;//创建一个临时指针temp,将head头结点的地址分配给temp
head = head->next;
delete tmp;
}
// 删除非头结点
ListNode* cur = head;
while (cur != NULL && cur->next!= NULL) {
if (cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
} else {
cur = cur->next;
}
}
return head;
}
};
方法2.设置一个虚拟头节点进行操作。
/**
* 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) {
//设置一个虚拟的头节点在进行移除节点操作
ListNode* dummyHead = new ListNode(0);
dummyHead->next=head;
ListNode *cur;
cur=dummyHead;
//将虚拟头结点指向head,这样方便后面做删除操作。
while(cur->next!=NULL){
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;
}
};
707.设计链表
1.while(--a)
表示在每次循环迭代中,会先将 a
减去1,然后检查 a
是否为非零值。如果 a
的初始值是0,那么在第一次循环迭代时,--a
将使 a
变为-1,因此条件 while(--a)
将评估为真,进入死循环。
class MyLinkedList {
public:
// 定义链表节点结构体
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
// 初始化链表
MyLinkedList() {
_dummyHead = new LinkedNode(0); // 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点
_size = 0;
}
// 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点
int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;
while(index--){ // 如果--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);
LinkedNode* cur=_dummyHead;
while(cur->next!=nullptr){
cur=cur->next;
}
cur->next = newNode;
_size++;
}
void addAtIndex(int index, int val) {
if(index>_size) return;
if(index<0) index=0;
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur=_dummyHead;
while(index--){
cur=cur->next;
}
newNode->next=cur->next;
cur->next=newNode;
_size++;
}
// 删除第index个节点,如果index 大于等于链表的长度,直接return,注意index是从0开始的
void deleteAtIndex(int index) {
if(index>=_size||index<0){
return;
}
LinkedNode* cur=_dummyHead;
while(index--){
cur=cur->next;
}
LinkedNode* tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
tmp=nullptr;
_size--;
}
//打印链表
void printLinkedList(){
LinkedNode* cur=_dummyHead;
while(cur->next!=nullptr){
cout<<cur->next->val<< " ";
cur=cur->next;
}
cout<<endl;
}
private:
int _size;
LinkedNode* _dummyHead;
};
206.反转链表
反转链表,不需要重新定义一个新链表,只需要将链表元素反转即可。即改变链表的next指针的指向。
双指针方法。
1.定义一个pre->nullptr以及一个cur->head;
2.
/**
* 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* pre=NULL;
ListNode* cur=head;
ListNode* temp;//保存cur的下一个节点
while(cur){
temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
return pre;
}
};