LeetCode—链表
1、T206 反转链表
/**
* 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 * q = NULL;
while(cur != NULL){
q = cur->next;
cur->next = pre;
pre = cur;
cur = q;
}
return 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) {
if(head == NULL || head->next == NULL){
return head;
}
ListNode * cur = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return cur;
}
};
2、T21 合并两个有序链表
/**
* 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode dummy(0);
ListNode* head = &dummy;
while(l1 != NULL && l2 != NULL){
if(l1->val < l2->val){
head->next = new ListNode(l1->val);
head = head->next;
l1 = l1->next;
}
else{
head->next = new ListNode(l2->val);
head = head->next;
l2 = l2->next;
}
}
while(l1 != NULL){
head->next = new ListNode(l1->val);
head = head->next;
l1 = l1->next;
}
while(l2 != NULL){
head->next = new ListNode(l2->val);
head = head->next;
l2 = l2->next;
}
return dummy.next;
}
};
3、T24 两两交换链表中的节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode dummy(0);
ListNode* tail = &dummy;
tail->next = head;
ListNode* p1 = tail;
while(p1->next != NULL && p1->next->next != NULL){
ListNode* p2 = p1->next;
ListNode* p3 = p2->next;
p1->next = p3;
p2->next = p3->next;
p3->next = p2;
p1 = p2;
}
return dummy.next;
}
};
4、T160 相交链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL) return NULL;
ListNode* pa = headA;
ListNode* pb = headB;
while(pa != pb){
(pa == NULL) ? (pa = headB) : (pa = pa->next);
(pb == NULL) ? (pb = headA) : (pb = pb->next);
}
return pa;
}
};
5、T234 回文链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head == NULL || head->next == NULL) return true;
ListNode* slow = head;
ListNode* fast = head;
while(fast->next != NULL && fast->next->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
ListNode* p1 = slow->next;
while(p1->next != NULL){
ListNode* p2 = p1->next;
p1->next = p2->next;
p2->next = slow->next;
slow->next = p2;
}
ListNode* pre = head;
while(slow->next != NULL){
slow = slow->next;
if(pre->val != slow->val) return false;
pre = pre->next;
}
return true;
}
};
6、T83 删除排序链表中的重复元素
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* p = head;
while(p != NULL && p->next != NULL){
if(p->val == p->next->val){
p->next = p->next->next;
}
else{
p = p->next;
}
}
return head;
}
};
7、T328 奇偶链表
/**
* 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* oddEvenList(ListNode* head) {
ListNode dummy1(0);
ListNode* tail1 = &dummy1;
ListNode dummy2(0);
ListNode* tail2 = &dummy2;
int flag = 1;
while(head != NULL){
if(flag == 1){
tail1->next = new ListNode(head->val);
tail1 = tail1->next;
head = head->next;
flag = 0;
}
else if(flag == 0){
tail2->next = new ListNode(head->val);
tail2 = tail2->next;
head = head->next;
flag = 1;
}
}
tail2->next = NULL;
tail1->next = dummy2.next;
return dummy1.next;
}
};
8、T19 删除链表的倒数第 N 个结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0);
ListNode* tail = &dummy;
tail->next = head;
ListNode* slow = tail;
ListNode* fast = tail;
while(n--){
fast = fast->next;
}
while(fast->next != NULL){
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummy.next;
}
};
9、T445 两数相加 II
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> s1;
stack<int> s2;
while(l1 != NULL){
s1.push(l1->val);
l1 = l1->next;
}
while(l2 != NULL){
s2.push(l2->val);
l2 = l2->next;
}
ListNode* p = new ListNode(0);
int num = 0;
while(!s1.empty() || !s2.empty()){
if(!s1.empty()){
num += s1.top();
s1.pop();
}
if(!s2.empty()){
num += s2.top();
s2.pop();
}
p->val = num % 10;
ListNode* q = new ListNode(num / 10);
q->next = p;
p = q;
num /= 10;
}
return p->val == 0 ? p->next : p;
}
};
10、T725 分隔链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
vector<ListNode*> res(k);
int len = 0;
for(ListNode* t = root; t != NULL; t = t->next)
len++;
int a = len / k;
int b = len % k;
for(int i = 0; i < k && root != NULL; ++i){
res[i] = root;
for(int j = 1; j < a + (i < b); ++j){
root = root->next;
}
ListNode* t = root->next;
root->next = NULL;
root = t;
}
return res;
}
};
9、T148 排序链表(未看)
/**
* 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* sortList(ListNode* head) {
return sortList(head, nullptr);
}
ListNode* sortList(ListNode* head, ListNode* tail) {
if (head == nullptr) {
return head;
}
if (head->next == tail) {
head->next = nullptr;
return head;
}
ListNode* slow = head, *fast = head;
while (fast != tail) {
slow = slow->next;
fast = fast->next;
if (fast != tail) {
fast = fast->next;
}
}
ListNode* mid = slow;
return merge(sortList(head, mid), sortList(mid, tail));
}
ListNode* merge(ListNode* head1, ListNode* head2) {
ListNode* dummyHead = new ListNode(0);
ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
while (temp1 != nullptr && temp2 != nullptr) {
if (temp1->val <= temp2->val) {
temp->next = temp1;
temp1 = temp1->next;
} else {
temp->next = temp2;
temp2 = temp2->next;
}
temp = temp->next;
}
if (temp1 != nullptr) {
temp->next = temp1;
} else if (temp2 != nullptr) {
temp->next = temp2;
}
return dummyHead->next;
}
};