链表算法专题
有关链表的题目,有一个常用的技巧:在头结点之前再建立一个虚拟结点,让虚拟结点指向头结点。
ListNode* dummyHead = new ListNode(-1);
dummyHead->next = head;
leetcode19
/**
* 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* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode();
dummyHead->next = head;
ListNode* p = dummyHead;
ListNode* q = head;
while (n--) {
q = q->next;
}
while (q) {
p = p->next;
q = q->next;
}
p->next = p->next->next;
return dummyHead->next;
}
};
leetcode237
把当前结点伪装成下一个结点,再把下一个点删掉。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
ListNode* tmp = node->next;
node->val = tmp->val;
node->next = tmp->next;
}
};
leetcode83
/**
* 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* deleteDuplicates(ListNode* head) {
ListNode* dummyHead = new ListNode(-1);
dummyHead->next = head;
ListNode* cur = head;
while (cur) {
while (cur->next && cur->val == cur->next->val) {
cur->next = cur->next->next;
}
cur = cur->next;
}
return dummyHead->next;
}
};
leetcode61
/**
* 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* rotateRight(ListNode* head, int k) {
if (head == NULL) {
return NULL;
}
int n = 0;
for (auto cnt = head; cnt; cnt = cnt->next) {
n++;
}
auto p = head, q = head;
k %= n;
while (k--) {
q = q->next;
}
while (q->next) {
p = p->next;
q = q->next;
}
q->next = head;
head = p->next;
p->next = NULL;
return head;
}
};
leetcode24
本题为两两交换相邻的结点,可设虚拟头结点dummyHead
,指针p
指向dummyHead
,指针a
和b
分别指向p->next
和p->next->next
,代码顺序为:
/**
* p->dummyHead;
* a = p->next;
* b = p->next->next
*/
p->next = b;
a->next = b->next;
b->next = a;
/**
* 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* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(-1);
dummyHead->next = head;
for (auto p = dummyHead; p->next && p->next->next; ) {
auto a = p->next, b = p->next->next;
p->next = b;
a->next = b->next;
b->next = a;
p = a;
}
return dummyHead->next;
}
};
leetcode206
/**
* 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* p = NULL, *q = head;
while (q) {
ListNode* tmp = q->next;
q->next = p;
p = q;
q = tmp;
}
return p;
}
};