203. 移除链表元素 - 力扣(LeetCode)
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]
容易出现操作空指针,要为对应的特殊情况做处理
直接在原本的链表上做处理。
分为两种情况
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head == NULL)return NULL;
ListNode* temp, * tempnext, * Head = head, * dele_val;
while (Head->val == val)
{
dele_val = Head;
if (Head->next != nullptr)Head = Head->next;
else return NULL;
delete dele_val;
}
temp = Head;
tempnext = temp->next;
if (tempnext == nullptr)return Head;
while (tempnext->next != nullptr)
{
if (tempnext->val == val)
{
temp->next = tempnext->next;
dele_val = tempnext;
delete dele_val;
tempnext = temp->next;
}
else
{
temp = temp->next;
tempnext = temp->next;
}
}
if (tempnext->val == val)
{
temp->next = nullptr;
delete tempnext;
}
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 *duny_head = new ListNode(0,head);
ListNode *cur = duny_head;
while(cur->next != nullptr)
{
if(cur->next->val == val)
{
ListNode *tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
}else
cur = cur->next;
}
ListNode *result = duny_head->next;
delete duny_head;
return result;
}
};
707. 设计链表 - 力扣(LeetCode)
你可以选择使用单链表或者双链表,设计并实现自己的链表。
单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。
如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。
实现 MyLinkedList 类:
输入
[“MyLinkedList”, “addAtHead”, “addAtTail”, “addAtIndex”, “get”, “deleteAtIndex”, “get”]
[[], [1], [3], [1, 2], [1], [1], [1]]
输出
[null, null, null, null, 2, null, 3]
解释
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2); // 链表变为 1->2->3
myLinkedList.get(1); // 返回 2
myLinkedList.deleteAtIndex(1); // 现在,链表变为 1->3
myLinkedList.get(1); // 返回 3
class MyLinkedList {
public:
struct Linknode
{
int val;
Linknode *next;
Linknode():val(0),next(nullptr){}
Linknode(int x):val(x),next(nullptr){}
Linknode(int x , Linknode* ptr):val(0),next(ptr){}
};
MyLinkedList() {
_dummy_head = new Linknode(0);
_size = 0;
}
int get(int index) {
if(index > _size-1 || index < 0) return -1;
Linknode *cur = _dummy_head->next;
while(index)
{
cur = cur->next;
index--;
}
return cur->val;
}
void addAtHead(int val) {
Linknode *tmp = new Linknode(val);
tmp->next = _dummy_head->next;
_dummy_head->next = tmp;
_size++;
}
void addAtTail(int val) {
Linknode *tmp = new Linknode(val);
Linknode *cur = _dummy_head;
while(cur->next != nullptr)
cur = cur->next;
cur->next = tmp;
_size++;
}
void addAtIndex(int index, int val) {
if(index > _size) return;
else if(index <= 0) addAtHead(val);
else if(index == _size) addAtTail(val);
else
{
Linknode *tmp = new Linknode(val);
Linknode *cur = _dummy_head;
while(index)
{
cur = cur->next;
index--;
}
tmp->next = cur->next;
cur->next = tmp;
_size++;
}
}
void deleteAtIndex(int index) {
if(index > _size-1 || index<0 ) return;
Linknode *cur = _dummy_head;
while(index)
{
cur = cur->next;
index--;
}
Linknode *tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size--;
}
private:
Linknode *_dummy_head;
int _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. 反转链表 - 力扣(LeetCode)
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
示例 2:
输入:head = [1,2]
输出:[2,1]
示例 3:
输入:head = []
输出:[]
链表中节点的数目范围是 [0, 5000]
-5000 <= Node.val <= 5000
链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?
处理分四种类型
#include
#include
#include
using namespace std;
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 ;
ListNode* temp1, * temp2 ;
if(head == nullptr || head->next == nullptr) return head;//处理链表为空或者长度为1
if(head->next->next == nullptr)//处理链表长度为2
{
temp1 = head;
temp2 = head->next;
temp2->next = temp1;
temp1->next = nullptr;
return temp2;
}
//处理链表长度为3及其以上
temp1 = head;
cur = temp1->next;
temp2 = cur->next;
while (temp2 != nullptr)
{
cur->next = temp1;
if (temp1 == head)
{
temp1->next = nullptr;
temp1 = cur;
}
else temp1 = cur;
cur = temp2;
if(cur->next != nullptr) temp2 = cur->next;//检查链表是否到底
else
{
cur->next = temp1;
break;
}
}
return cur;
}
};
int main()
{
vector<int> head = { 1,2,3,4,5,6 };
ListNode* head_test = new ListNode(0);
ListNode* test , *cur = head_test;
Solution a;
for (int i = 0; i < head.size(); i++)
{
ListNode* temp = new ListNode(head[i]);
cur->next = temp;
cur = cur->next;
}
cur->next = nullptr;
cur = head_test;
cout << "cur list" << endl;
while (cur->next != nullptr)
{
cout << cur->val << ' ';
cur = cur->next;
}
cout << cur->val << endl;
test = a.reverseList(head_test->next);
while (test->next != nullptr)
{
cout << test->val << ' ';
test = test->next;
}
cout << test->val << ' ';
return 0;
}
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur ;
ListNode* temp1, * temp2 ;
cur = head;
temp1 = nullptr;
while (cur)
{
temp2 = cur->next;
cur->next = temp1;
temp1 = cur;
cur = temp2;
}
return temp1;
}
};
class Solution {
public:
ListNode* reverse(ListNode *pre , ListNode * cur) {
if (cur == nullptr)return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur , temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(nullptr , head);
}
};