203.移除連結串列元素
題目連結:https://leetcode.cn/problems/remove-linked-list-elements/description/
我的程式碼(分頭節點和中間節點兩種情況操作):
/**
* 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 != nullptr && head->val == val) {
ListNode* q = head;
head = head->next;
delete q;
}
ListNode* p = head;
while (p != nullptr && p->next != nullptr) {
if (p->next->val == val) {
ListNode* q = p->next;
p->next = q->next;
delete q;
} else
p = p->next;
}
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* dummy_head = new ListNode;
dummy_head->next = head;
ListNode* current = dummy_head;
while (current->next != nullptr) {
if (current->next->val == val) {
ListNode* temp = current->next;
current->next = current->next->next;
delete temp;
} else
current = current->next;
}
head = dummy_head->next;
delete dummy_head;
return head;
}
};
在原連結串列的頭節點之前再加一個實際上不儲存資料的虛擬頭節點,方便操作。
707.設計連結串列
題目連結:https://leetcode.cn/problems/design-linked-list/description/
我的程式碼:
class MyLinkedList {
public:
struct LinkedList {
int val;
struct LinkedList* next;
LinkedList(int val) : val(val), next(nullptr) {}
};
MyLinkedList() {
dummy_head = new LinkedList(0);
size = 0;
}
int get(int index) {
if (index < size && index >= 0) {
LinkedList* p = dummy_head->next;
while (index > 0) {
index--;
p = p->next;
}
return p->val;
} else
return -1;
}
void addAtHead(int val) {
LinkedList* p = new LinkedList(val);
p->next = dummy_head->next;
dummy_head->next = p;
size++;
}
void addAtTail(int val) {
LinkedList* p = new LinkedList(val);
LinkedList* q = dummy_head;
int length = size;
while (length > 0) {
length--;
q = q->next;
}
q->next = p;
p->next = nullptr;
size++;
}
void addAtIndex(int index, int val) {
if (index >= 0 && index < size) {
LinkedList* p = new LinkedList(val);
LinkedList* q = dummy_head;
while (index > 0) {
index--;
q = q->next;
}
p->next = q->next;
q->next = p;
size++;
} else if (index == size) {
LinkedList* p = new LinkedList(val);
LinkedList* q = dummy_head;
while (index > 0) {
index--;
q = q->next;
}
q->next = p;
p->next = nullptr;
size++;
}
}
void deleteAtIndex(int index) {
LinkedList* p = dummy_head;
if (index < size && index >= 0) {
while (index > 0) {
index--;
p = p->next;
}
LinkedList* q = p->next;
p->next = q->next;
delete q;
size--;
}
}
private:
LinkedList* 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.反轉連結串列
題目連結:https://leetcode.cn/problems/reverse-linked-list/description/
雙指標解法:
/**
* 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 = nullptr;
ListNode* current = head;
while (current != nullptr) {
ListNode* temp = current->next;
current->next = pre;
pre = current;
current = temp;
}
return pre;
}
};
pre:開始定義為頭節點前的空指標。
current:開始定義為頭節點。
每次迴圈使用temp暫存current的next節點,然後使current的next指標指向pre,pre和current都後移,最後當current指向尾節點後的空指標時結束迴圈,返回當前真正頭節點pre。
遞迴解法:
/**
* 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) { return reverse(nullptr, head); }
ListNode* reverse(ListNode* pre, ListNode* current) {
if (current == nullptr)
return pre;
ListNode* temp = current->next;
current->next = pre;
return reverse(current, temp);
}
};
與雙指標解法思路相同。