题目链接:203. 移除链表元素 - 力扣(LeetCode)
思路:
虚拟头结点
关键点:待删除结点,先用tmp指针另存,然后手动释放
// C++代码
/**
* 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(0);
dummy_head->next = head;
ListNode* cur = dummy_head;
while (cur->next) {
if (cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next= cur->next->next;
delete tmp;
}
else {
cur = cur->next;
}
}
head = dummy_head->next;
delete dummy_head;
return head;
}
};
# python代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy_head = ListNode(next=head)
cur = dummy_head
while cur.next :
if cur.next.val == val :
cur.next = cur.next.next
else :
cur = cur.next
return dummy_head.next
文章讲解:代码随想录 (programmercarl.com)
视频讲解:帮你把链表操作学个通透!LeetCode:707.设计链表_哔哩哔哩_bilibili
链表的基本操作:
关键点:插入时先考虑后面的节点,在考虑前面的节点;
// C++代码
class MyLinkedList {
public:
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode() : val(0), next(nullptr) {}
LinkedNode(int x) : val(x), next(nullptr) {}
LinkedNode(int x, LinkedNode* next) : val(x), next(next) {}
};
MyLinkedList() {
_dummy_head = new LinkedNode(0);
_size = 0;
}
int get(int index) {
if (index > (_size - 1) || index < 0) return -1;
LinkedNode* cur = _dummy_head;
while (index--) {
cur = cur->next;
}
return cur->next->val;
}
void addAtHead(int val) {
LinkedNode* new_node = new LinkedNode(val);
new_node->next = _dummy_head->next;
_dummy_head->next = new_node;
_size++;
}
void addAtTail(int val) {
LinkedNode* new_node = new LinkedNode(val);
LinkedNode* cur = _dummy_head;
while (cur->next != nullptr) {
cur = cur->next;
}
new_node->next = cur->next;
cur->next = new_node;
_size++;
}
void addAtIndex(int index, int val) {
if (index > _size) return;
if (index < 0) index = 0;
LinkedNode* new_node = new LinkedNode(val);
LinkedNode* cur = _dummy_head;
while (index--) {
cur = cur->next;
}
new_node->next = cur->next;
cur->next = new_node;
_size++;
}
void deleteAtIndex(int index) {
if (index < 0 || index > (_size - 1)) return;
LinkedNode* cur = _dummy_head;
while (index--) {
cur = cur->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
tmp = nullptr;
_size--;
}
// void printLinkedList() {
// LinkedNode* cur = _dummy_head;
// while (cur->next != nullptr) {
// cout << cur->next->val << " ";
// cur = cur->next;
// }
// cout << endl;
// cout << get(0) << endl;
// cout << get(1) << endl;
// cout << get(2) << endl;
// }
private:
LinkedNode* _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)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:帮你拿下反转链表 | LeetCode:206.反转链表 | 双指针法 | 递归法_哔哩哔哩_bilibili
双指针法
关键点:注意tmp
// C++代码
/**
* 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 = NULL;
ListNode* cur = head;
ListNode* tmp = NULL;
while (cur) {
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
#python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
pre = None
cur = head
tmp = None
while cur :
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre