【算法训练营day3】LeetCode203. 移除链表元素 707. 设计链表 206. 反转链表
LeetCode203. 移除链表元素
题目链接:203. 移除链表元素
初次尝试
题目比较简单,之前刷过链表的题,有一段时间不碰了,刚开始对语法有一点生疏,后面找到感觉了,一遍ac。
/**
* 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* newHead = new ListNode(0, head);
ListNode* p = newHead;
ListNode* temp;
while (p -> next != NULL) {
if (p -> next -> val == val) {
temp = p -> next;
p -> next = temp -> next;
delete temp;
}
else p = p -> next;
}
return newHead -> next;
}
};
看完代码随想录后的想法
思路是一样的,感觉虚拟头节点的代码会更简单,思考量也会更小。
LeetCode707. 设计链表
题目链接:707. 设计链表
初次尝试
思考量不大的一道题,比较考验基本功,一遍ac。
class MyLinkedList {
public:
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val) : val(val), next(nullptr) {}
};
MyLinkedList() {
_newHead = new LinkedNode(0);
_size = 0;
}
int get(int index) {
LinkedNode* p = _newHead;
for (; index >= 0; index--) {
p = p -> next;
if (p == NULL) return -1;
}
return p -> val;
}
void addAtHead(int val) {
LinkedNode* newNode = new LinkedNode(val);
newNode -> next = _newHead -> next;
_newHead -> next = newNode;
_size++;
}
void addAtTail(int val) {
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* p = _newHead;
for (int i = 0; i < _size; i++) {
p = p -> next;
}
p -> next = newNode;
_size++;
}
void addAtIndex(int index, int val) {
if (index < 0) addAtHead(val);
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* p = _newHead;
for (; index > 0; index--) {
p = p -> next;
if (p == NULL) return;
}
newNode -> next = p -> next;
p -> next = newNode;
_size++;
}
void deleteAtIndex(int index) {
LinkedNode* p = _newHead;
LinkedNode* temp;
for (; index > 0; index--) {
p = p -> next;
if (p -> next == NULL) return;
}
temp = p -> next;
p -> next = temp -> next;
delete temp;
_size--;
}
private:
LinkedNode* _newHead;
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);
*/
看完代码随想录后的想法
思路一样。
LeetCode206. 反转链表
题目链接:206. 反转链表
初次尝试
我的想法是建一个新的链表,然后遍历旧链表,依次插入到新链表中,一遍ac。
/**
* 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) {
if (head == NULL) return NULL;
ListNode* ans = new ListNode();
ListNode* p = head;
ListNode* temp = p -> next;
while (p != NULL) {
p -> next = ans -> next;
ans -> next = p;
p = temp;
if (temp == NULL) break;
temp = temp -> next;
}
return ans -> next;
}
};
看完代码随想录后的想法
题解的方法非常巧妙,确实只需要两个指针遍历链表将next指针反向就行了。
/**
* 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 = NULL;
ListNode* temp;
while (cur) {
temp = cur -> next;
cur -> next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
标签:ListNode,val,int,707,next,链表,移除,LinkedNode
From: https://www.cnblogs.com/BarcelonaTong/p/16793264.html