203.移动链表元素
题目链接:
https://leetcode.cn/problems/remove-linked-list-elements/
解题方法一:
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* cur = head;
//删除头节点
for(;head!= NULL && head->val == val;)
{
head = head->next;
}
//删除非头节点
for(;cur!= NULL && cur->next != NULL;)
{
if(cur ->next-> val == val )
{
cur->next = cur->next->next;
}
else {
cur = cur->next;
}
}
return head;
}
};
解题方法二:设置虚拟链表头节点
涉及增加或删除节点可以使用该方法
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* tempNode = (ListNode*)malloc(sizeof(ListNode));
tempNode->next = head;
tempNode->val = 0;
ListNode* cur = tempNode;
//删除非头节点
for(;cur!= NULL && cur->next != NULL;)
{
if(cur ->next-> val == val )
{
cur->next = cur->next->next;
}
else {
cur = cur->next;
}
}
head = tempNode->next;
free(tempNode);
return head;
}
};
707.设计链表
题目链接
https://leetcode.cn/problems/design-linked-list/
实现链表:获取链表指定节点的值、在链表头节点或尾节点插入新节点、在指定位置添加新节点、删除指定节点
单链表:
解题方法一:
这个代码执行不通过
提示:
class MyLinkedList {
public:
struct LinkedList{
int val;
LinkedList * next;
};
MyLinkedList() {
_dummyHead = (LinkedList *)malloc(sizeof(LinkedList));
_dummyHead->next = NULL;
_dummyHead->val = 0;
_size = 0;
}
int get(int index) {
if(index > _size - 1 || index < 0)
{
return -1;
}
LinkedList * cur = _dummyHead->next;
while(index --) // --index和index区别
{
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkedList * Head = (LinkedList *)malloc(sizeof(LinkedList));
Head->next = _dummyHead;
Head->val = val;
_size++;
}
void addAtTail(int val) {
LinkedList * cur = _dummyHead;
while(cur->next != NULL)
{
cur = cur->next;
}
LinkedList *tail = (LinkedList *)malloc(sizeof(LinkedList));
tail->next = NULL;
tail->val = val;
cur->next = tail;
_size++;
}
void addAtIndex(int index, int val) {
if(index > _size - 1 || index < 0)
{
//return -1;
}
if(index == 0)
{
LinkedList *Head = (LinkedList *)malloc(sizeof(LinkedList));
Head->next = _dummyHead;
Head->val = val;
_size++;
}
else
{
LinkedList * cur = _dummyHead;
while(index--)
{
cur = cur->next;
}
LinkedList *temp = (LinkedList *)malloc(sizeof(LinkedList));
temp->next = cur->next;
cur->next = temp;
temp->val = val;
_size++;
}
}
void deleteAtIndex(int index) {
if(index > _size - 1 || index < 0)
{
//return -1;
}
if(index == 0)
{
_dummyHead = _dummyHead->next;
_size--;
}
else
{
LinkedList * cur = _dummyHead;
while(index --)
{
cur = cur->next;
}
cur->next = cur->next->next;
_size--;
}
}
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);
*/
class MyLinkedList {
public:
struct LinkedList{
int val;
LinkedList * next;
};
MyLinkedList() {
_dummyHead = (LinkedList *)malloc(sizeof(LinkedList));
_dummyHead->next = NULL;
_dummyHead->val = 0;
_size = 0;
}
int get(int index) {
if(index > (_size - 1) || index < 0)
{
return -1;
}
LinkedList* cur = _dummyHead->next;
while(cur->next != NULL && index--){ // 如果--index 就会陷入死循环
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkedList * Head = (LinkedList *)malloc(sizeof(LinkedList));
Head->next = _dummyHead;
Head->val = val;
_size++;
}
void addAtTail(int val) {
LinkedList * cur = _dummyHead;
while(cur->next != NULL)
{
cur = cur->next;
}
LinkedList *tail = (LinkedList *)malloc(sizeof(LinkedList));
tail->next = NULL;
tail->val = val;
cur->next = tail;
_size++;
}
void addAtIndex(int index, int val) {
if(index > _size - 1 || index < 0)
{
//return -1;
}
if(index == 0)
{
LinkedList *Head = (LinkedList *)malloc(sizeof(LinkedList));
Head->next = _dummyHead;
Head->val = val;
_size++;
}
else
{
LinkedList * cur = _dummyHead;
while(index--)
{
cur = cur->next;
}
LinkedList *temp = (LinkedList *)malloc(sizeof(LinkedList));
temp->next = cur->next;
cur->next = temp;
temp->val = val;
_size++;
}
}
void deleteAtIndex(int index) {
if(index > _size - 1 || index < 0)
{
//return -1;
}
if(index == 0)
{
_dummyHead = _dummyHead->next;
_size--;
}
else
{
LinkedList * cur = _dummyHead;
while(index --)
{
cur = cur->next;
}
cur->next = cur->next->next;
_size--;
}
}
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.反转链表
https://leetcode.cn/problems/reverse-linked-list/
解题方法一:
/**
* 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;
ListNode* pre = NULL;
while(cur) {
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};