文章目录
一.排序:
二.链表
往链表的末尾添加元素
ListNode *AddToTail(ListNode *pHead, int value) {
ListNode *pNew = new ListNode();
pNew->val = value;
pNew->next = nullptr;
if (pHead == nullptr) {
return pNew;
} else {
ListNode *p = pHead;
while(p->next) {
p = p->next;
}
p->next = pNew;
}
return pHead;
}
从尾到头打印链表
void PrintListReversingly(ListNode *head) {
std::stack<int> s;
ListNode *p = head;
while(p != nullptr) {
s.push(p->val);
p = p->next;
}
while(!s.empty()) {
printf("%d\t", s.top());
s.pop();
}
}
合并两个有序链表
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1;
if(l1->val < l2->val) {
l1->next=mergeTwoLists(l1->next,l2);
return l1;
} else {
l2->next=mergeTwoLists(l1,l2->next);
return l2;
}
}
};
反转链表
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
if(pHead == NULL || pHead->next == NULL) return pHead;
struct ListNode* p = ReverseList(pHead->next);
pHead->next->next = pHead;
pHead->next = NULL;
return p;
}
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *pre = nullptr, *cur = pHead, *last;
while(cur) {
last = cur->next;
cur->next = pre;
pre = cur;
cur = last;
}
return pre;
}
};
判断链表是否是回文链表
class Solution {
public:
ListNode *reverse(ListNode *head) {
ListNode *p = head, *q, *pre = NULL;
while(p){
q = p->next;
p->next = pre;
pre = p;
p = q;
}
return pre;
}
bool isPalindrome(ListNode* head) {
ListNode *fast = head, *last = head;
while (fast && fast->next) {
last = last->next;
fast = fast->next;
fast = fast->next;
}
if (fast) last = last->next;
last = reverse(last);
fast = head;
while(last && fast) {
if (last->val != fast->val) return false;
fast = fast->next;
last = last->next;
}
return true;
}
};
链表相交
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode *p = headA, *q = headB;
while (p != q) {
p = p ? p ->next : headB;
q = q ? q ->next : headA;
}
return p;
}
链表的环
快指针p, 慢指针q;
环的起始位置为o, 相遇位置为x, 从起点到环的起始位置长la, 环长lc;1.la < lc
当慢指针q到环的起始位置o时,慢指针q走了la长,快指针p已经走了2*la,此时快指针距离起始位置o还有lb, 得la + lb = lc,现在两者都在环上, 接下来的运动轨迹,可以视为追击问题, 每次运动,快指针p与慢指针q的距离-1,相遇需运动lb次,此时慢指针q走了lb, 在往后走la步可到环的起点(注:la + lb = lc);相遇后让快指针p到起点