链表简介
使用题目提供的链表结构,如下所示。使用初始化列表完成初始化。
//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) {}
};
203.移除链表元素
- 使用头节点,统一原头节点和其他节点的删除操作。
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead = new ListNode(0, head);
ListNode* pre = dummyHead;
while (pre->next != nullptr) {
if (pre->next->val == val) {
ListNode* tmp = pre->next;
pre->next = pre->next->next;
delete tmp;
} else {
pre = pre->next;
}
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};
707.设计链表
- 使用虚拟头节点。注意插入删除区间选择,以及长度维护。
class MyLinkedList {
public:
struct LinkedNode {
int val;
LinkedNode* next;
// LinkedNode():val(0),next(nullptr){}
LinkedNode(int v) : val(v), next(nullptr) {}
// LinkedNode(int v,LinkedNode n):val(v),next(n){}
};
MyLinkedList() {
_dummyNode = new LinkedNode(0);
_size = 0;
}
int get(int index) {
if (index < 0 || index >= _size)
return -1;
LinkedNode* pre = _dummyNode;
while (index--) {
pre = pre->next;
}
return pre->next->val;
}
void addAtHead(int val) {
LinkedNode* head = new LinkedNode(val);
head->next = _dummyNode->next;
_dummyNode->next = head;
++_size;
}
void addAtTail(int val) {
LinkedNode* tail = new LinkedNode(val);
LinkedNode* pre = _dummyNode;
while (pre->next != nullptr) {
pre = pre->next;
}
pre->next = tail;
++_size;
}
void addAtIndex(int index, int val) {
if (index < 0 || index > _size)
return;
LinkedNode* in = new LinkedNode(val);
LinkedNode* pre = _dummyNode;
while (index--) {
pre = pre->next;
}
in->next = pre->next;
pre->next = in;
++_size;
}
void deleteAtIndex(int index) {
if (index < 0 || index >= _size)
return;
LinkedNode* pre = _dummyNode;
while (index--) {
pre = pre->next;
}
LinkedNode* tmp = pre->next;
pre->next = pre->next->next;
delete tmp;
--_size;
}
private:
LinkedNode* _dummyNode;
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.反转链表
- 理解临时指针
tmp
的使用,tmp
指向脱离的部分tmp = cur->next
。
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = nullptr;
ListNode* cur = head;
while (cur != nullptr) {
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
24.两两交换链表中的元素
- 与206.反转链表相比,多一个指针,主要在与如何维护这三个指针
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode* dummyHead = new ListNode(0, head);
ListNode* pre = dummyHead;
ListNode* cur = head;
ListNode* nex = head->next;
while (cur != nullptr && cur->next != nullptr) {
ListNode* tmp = nex->next;
pre->next = nex;
cur->next = tmp;
nex->next = cur;
pre = cur;
cur = tmp;
if (cur != nullptr)
nex = cur->next;
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};
19.删除链表的倒数第N个元素
- 双指针法,让右指针走到最后一个元素,左指针与它相差
N-1
位即可,需要删除节点使用preLe
指向待删除节点的前一节点。
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int diff = n - 1;
ListNode* dummyHead = new ListNode(0, head);
ListNode* preLe = dummyHead;//待删除节点的前一节点
ListNode* ri = head;
while (diff--) {
ri = ri->next;
}
while (ri->next != nullptr) {
ri = ri->next;
preLe = preLe->next;
}
ListNode* tmp = preLe->next;
preLe->next = preLe->next->next;
delete tmp;
head = dummyHead->next;
delete dummyHead;
return head;
}
};
160.链表相交
- 使用双指针,先遍历一遍得出各自长度,再置于同一起跑线移动。
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* ptrA = headA;
ListNode* ptrB = headB;
int lenA = 0;
int lenB = 0;
while (ptrA != nullptr) {
ptrA = ptrA->next;
lenA++;
}
while (ptrB != nullptr) {
ptrB = ptrB->next;
lenB++;
}
ptrA = headA;
ptrB = headB;
if (lenB > lenA) {
swap(ptrA, ptrB);
swap(lenA, lenB);
}
int diff = lenA - lenB;
while (diff--) {
ptrA = ptrA->next;
}
while (ptrA != nullptr) {
if (ptrA == ptrB)
return ptrA;
ptrA = ptrA->next;
ptrB = ptrB->next;
}
return nullptr;
}
};