203.移除链表元素
有两种方法,一是移动头节点删除,一是使用虚拟节点删除
对于我来说 重点在于 构建链表以及初始化
第一方法解答如下:
/**
* 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)
{
ListNode *temp = head;
head = head-> next;
delete temp;
}
//非头节点
ListNode* cur =head;
while(cur!= NULL && cur->next !=NULL)
{
if(cur->next->val == val)
{
ListNode* temp = cur -> next; //为了方便记录现在的cur->next;为了方便后续删除
cur->next = cur->next->next; //这里如果用temp的话 会再次读取temp 虽然能过 但是时间会更久
delete temp;
}
else
{
cur = cur->next; //遍历推进 等同于i++作用 更新cur继续遍历
}
}
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) {
ListNode* dummynode = new ListNode(0);
dummynode ->next = head ;
ListNode* cur = dummynode;
while(cur->next != NULL)
{
if(cur->next->val == val){
ListNode *temp = cur -> next;
cur->next = cur->next->next;
delete temp;
}
else
cur = cur->next;
}
head = dummynode->next;
delete dummynode;
return head;
}
};
707.设计链表
看教程吧 比较基础 但是很复杂 有些边界问题 和 private问题 容易出错的是一些条件
class MyLinkedList {
public:
struct LinkedList{
LinkedList *next;
int val;
LinkedList (int x):val(x),next(NULL){}
};
MyLinkedList() { //初始化·为一个·空节点;
_dummyhead = new LinkedList(0);
_size =0;
}
int get(int index) {
if(index>(_size-1)||index<0){return -1;}
LinkedList *cur = _dummyhead -> next;//
//声明一个节点为 当前节点 然后根据传入的index与默认初始化的——size对比 循环
while(index--){ //循环index次 到达index1下标 如果用-- index就会陷入死循环
cur = cur->next; //将当前cur循环到index处
}
return cur->val;
}
void addAtHead(int val) {
LinkedList *newnode = new LinkedList(val);
newnode ->next = _dummyhead->next;
_dummyhead ->next = newnode;
_size++;
}
void addAtTail(int val) {
LinkedList *newnode = new LinkedList(val);
LinkedList *cur = _dummyhead;
while(cur->next!=NULL){
cur = cur->next;
}
cur->next= newnode;
_size++;
}
void addAtIndex(int index, int val) {
if(index > _size) return;
if(index < 0) index = 0;
LinkedList *newnode = new LinkedList(val);
LinkedList *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;
}
LinkedList *cur = _dummyhead;
while(index--){
cur = cur->next;
}
LinkedList *temp = cur->next; //声明一个临时节点 存储原来的节点指针 最后再删除释放
cur -> next = cur->next->next;
delete temp;
//delete命令指示释放了tmp指针原本所指的那部分内存,
//被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
//如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
//如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
temp=nullptr;
_size--;
}
void printLinkedList() {
LinkedList* cur = _dummyhead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int _size;
LinkedList* _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);
*/
206.反转链表
题目不难
/**
* 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 *temp ;
ListNode *cur = head; //双指针 定义cur指针 指向head head是节点 cur是指向它的指针
ListNode *pre = NULL; //另一个指针 存放节点的前面的指针
while(cur) //不停的循环cur 使其指针指向前面
{
temp = cur->next; //保存一下cur的下一个节点,因为接下来要改变cur->next;
cur->next = pre; //翻转操作
//更新指针 pre和 cur
pre = cur; //cur是cur->next的上一个节点 所以等于pre
cur = temp; //
}
return pre;
}
};