分类:链表
@ 链表基础操作:
Node* createList(int len);
Node* createList(int a[], int len);
void destroyList(Node* head);
Node* pushNode(Node* head, int num) ;
Node* insertNode(Node* head, int pos, int num) ;
void deleteNode(Node* head, int pos) ;
Node* deleteNode(Node* head, int num) ;
Node* modifyNode(Node* head, int src, int dst) ;
Node* mergeList(Node* h1, Node* h2) ;
int listCount(Node* head) ;
@链表翻转
/**
* 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* ph = NULL, *p = head, *q = head;
while(p) {
q = p;
p = p->next;
q->next = ph;
ph = q;
}
return ph;
}
};
@链表中第K个节点
Node* findKNode(Node* head, int k);
@复杂链表的复制
Node* cloneList(Node* head);
@链表中环的入口
Node* cirList(Node* head);
@删除链表中重复的节点
Node* delDupNode(Node* head);
@两个链表的第一个公共节点
Node* firstComNode(Node* h1, Node* h2);
@双向链表排序
@两数相加
/**
* 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) {
ListNode* pHead = NULL, *p = NULL, *q = NULL;
int carry = 0, sum = 0;
while(l1 || l2) {
int a1 = l1 ? l1->val : 0;
int a2 = l2 ? l2->val : 0;
sum = a1 + a2;
q = new ListNode((sum + carry) % 10);
carry = (sum + carry) / 10;
if(pHead){
p->next = q;
p = p->next;
}else{
pHead = q;
p = pHead;
}
if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
q = NULL;
if(carry){
q = new ListNode(carry);
q->next = NULL;
}
p->next = q;
return pHead;
}
};
@删除链表的倒数第 N 个结点
/**
* 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) {
if(head == nullptr || n <= 0) {
return nullptr;
}
ListNode *p = head;
for(int i = 0; i < n; i++) {
if(p) {
p = p->next;
} else {
return nullptr;
}
}
ListNode *q = head, *r = head;
while(p) {
r = q;
p = p->next;
q = q->next;
}
if(q != head) {
r->next = q->next;
delete q;
} else {
head = head->next;
delete q;
}
return head;
}
};
@返回链表的倒数第 N 个结点
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
if(pHead == nullptr || k <= 0) {
return nullptr;
}
ListNode *p = pHead;
for(int i = 0; i < k; i++) {
if(p) {
p = p->next;
} else {
return nullptr;
}
}
ListNode *q = pHead;
while(p) {
p = p->next;
q = q->next;
}
return q;
}
};
@合并两个有序链表
方法1:递归
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == nullptr) {
return l2;
} else if (l2 == nullptr) {
return l1;
} else if (l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
方法2:迭代
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* preHead = new ListNode(-1);
ListNode* prev = preHead;
while (l1 != nullptr && l2 != nullptr) {
if (l1->val < l2->val) {
prev->next = l1;
l1 = l1->next;
} else {
prev->next = l2;
l2 = l2->next;
}
prev = prev->next;
}
prev->next = l1 == nullptr ? l2 : l1;
return preHead->next;
}
};
@对链表插入排序
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == nullptr) {
return head;
}
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* lastSorted = head;
ListNode* curr = head->next;
while (curr != nullptr) {
if (lastSorted->val <= curr->val) {
lastSorted = lastSorted->next;
} else {
ListNode *prev = dummyHead;
while (prev->next->val <= curr->val) {
prev = prev->next;
}
lastSorted->next = curr->next;
curr->next = prev->next;
prev->next = curr;
}
curr = lastSorted->next;
}
return dummyHead->next;
}
};
@合并K个排序链表
class Solution {
public:
struct Status {
int val;
ListNode *ptr;
bool operator < (const Status &rhs) const {
return val > rhs.val;
}
};
priority_queue <Status> q;
ListNode* mergeKLists(vector<ListNode*>& lists) {
for (auto node: lists) {
if (node) q.push({node->val, node});
}
ListNode head, *tail = &head;
while (!q.empty()) {
auto f = q.top(); q.pop();
tail->next = f.ptr;
tail = tail->next;
if (f.ptr->next) q.push({f.ptr->next->val, f.ptr->next});
}
return head.next;
}
};