濮陽做網(wǎng)站的公司有哪些谷歌搜索引擎下載
● 自己看到題目的第一想法
203.移除鏈表元素
方法一:
- 思路:
設(shè)置虛擬頭節(jié)點 dummyhead
設(shè)置臨時指針 cur 遍歷 整個鏈表
循環(huán):
-
如果 cur !=nullptr &&cur->next !=nullptr 則 遍歷鏈表 否則結(jié)束遍歷
-
如果 cur->next == val 則 cur->next = cur->next->next
-
如果 cur->next !=val 則 cur = cur->next
返回 return dummyhead->next
- 注意:用while循環(huán)
- 代碼:
/*** 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 = dummyhead;while(cur !=nullptr &&cur->next !=nullptr){if(cur->next->val == val){cur->next = cur->next->next;}else{cur = cur->next;}}head = dummyhead->next;delete dummyhead;return head;}
};
- 運行結(jié)果:
方法二:
-
思路:
直接在原鏈表上操作1.頭節(jié)點是val值
刪除頭節(jié)點 head = head->next;2.頭節(jié)點不是val值
定義一個臨時變量cur 遍歷整個鏈表
循環(huán) :
-
如果cur !=nullptr && cur->next !=nullptr 則 遍歷鏈表 否則結(jié)束遍歷
-
如果 cur->next == val 則 cur->next = cur->next->next
-
如果 cur->next !=val 則 cur = cur->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) {while(head !=nullptr && head->val == val){head = head->next;}ListNode *cur = head;while(cur !=nullptr && cur->next !=nullptr){if(cur->next->val == val ){cur->next = cur->next->next;}else{cur = cur->next;}}return head;}
};
- 運行結(jié)果:
707.設(shè)計鏈表
- 思路:
- 注意:
cur應(yīng)該指向_dummyhead 還是_dummyhead->next;
鏈表的構(gòu)造struct 還有 private中 鏈表的 定義 - 代碼:
class MyLinkedList {
public:
struct ListNode{int val;ListNode* next ;ListNode(int val): val(val), next(nullptr){}
};MyLinkedList() {_size = 0;_dummyhead = new ListNode(0);}int get(int index) {if(index>(_size-1) || index<0){return -1;}ListNode* cur = _dummyhead;while(index){cur = cur->next;index--;}return cur->next->val;}void addAtHead(int val) {ListNode* newnode = new ListNode(val);newnode->next = _dummyhead->next;_dummyhead->next = newnode;_size++;}void addAtTail(int val) {ListNode* cur = _dummyhead;ListNode* newnode = new ListNode(val);while(cur !=nullptr && cur->next !=nullptr){cur =cur->next;}cur->next = newnode;_size++;}void addAtIndex(int index, int val) {ListNode* newnode = new ListNode(val);if(index<0) index =0;if(index >_size) return ;ListNode * cur = _dummyhead;while(index--){cur = cur->next;}newnode->next = cur->next;cur->next = newnode;_size++;}void deleteAtIndex(int index) {if(index<0 || index>(_size-1)){return ;}ListNode*cur = _dummyhead;while(index--){cur = cur->next;}cur->next = cur->next->next;_size--;}private:int _size;ListNode* _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);*/
- 運行結(jié)果:
206.反轉(zhuǎn)鏈表
方法一:
-
思路:雙指針
定義pre= null, cur = head, 臨時變量temp保存 cur->next;
循環(huán):cur != null讓cur->next = pre; pre = cur; cur = temp;
返回: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) {ListNode* cur = head;ListNode* pre = nullptr;ListNode* tmp ;while(cur !=nullptr){tmp = cur->next;cur->next = pre ;pre =cur;cur = tmp;}return pre;}
};
-
運行結(jié)果
方法二: -
思路:遞歸法:
先完成翻轉(zhuǎn)的第一步:
確定終止條件: cur==null 返回 pre
循環(huán)體: cur ->next = pre
遞歸下去 return reverse(cur, tmp) -
注意:
-
代碼:
/*** 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* reverse(ListNode* pre, ListNode* cur ){if(cur == nullptr) return pre;ListNode* temp;temp = cur->next;cur->next = pre;return reverse(cur, temp);}ListNode* reverseList(ListNode* head) {return reverse(nullptr, head);}
};
- 運行結(jié)果: