两个链表的第一个公共结点
双指针
使用两个指针p和q分别指向两个链表head1,head2的头结点,然后同时分别逐结点遍历,当p到达链表head1的末尾时,重新定位到链表head2的头结点;当q到达链表head2的末尾时,重新定位到链表head1的头结点。这样,当p和q相遇时,所指向的结点就是第一个公共结点。设head1到公共节点长度为a,head2到公共节点长度为b,则a+b=b+a,拼接后则head1,2到公共节点长度相等
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
ListNode *p=pHead1, *q=pHead2;
while(p!=q){
if(p==NULL) p=pHead2;
else p=p->next;
if(q==NULL) q=pHead1;
else q=q->next;
}
return p;
}
};
栈:我们将链表的节点装入栈中,比较顶部的节点,然后遍历栈查找公共节点。
链表长度差:分别遍历两个链表headA和headB,如果指针相同,则直接返回,否则,计算链表长度差值,我们使用变量step来计算长链表比短链表多出的长度,接下来,我们让长链表先跑step步,然后链表headA和headB同步移动,查找公共节点。
链表相加(二)
反转两个链表再相加
翻转后从头开始相加,用一个整型变量记录进位值,使用头插法存结果,最后翻转结果
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
ListNode* addInList(ListNode* head1, ListNode* head2) {
// write code here
head1=reverse(head1);
head2=reverse(head2);
ListNode *dummy = new ListNode(0);
ListNode *cur=dummy;
int jw=0;//进位
while (head1 || head2 || jw) {
int sum = jw;
if(head1){
sum += head1->val;
head1=head1->next;
}
if(head2){
sum += head2->val;
head2=head2->next;
}
jw = sum /10;
sum %= 10;
cur->next=new ListNode(sum);
cur=cur->next;
}
ListNode *res=reverse(dummy->next);//结果翻转
return res;
}
ListNode* reverse(ListNode *head){
ListNode *pre=NULL, *cur=head;
while(cur!=NULL){
ListNode *next=cur->next;
cur->next=pre;
pre=cur,cur=next;
}
return pre;
}
};
单链表的排序
归并排序
将无序列表分为左右两个,递归分解,则最终每个可以看做一个有序列表,然后两两合并有序列表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <algorithm>
class Solution {
public:
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
ListNode* sortInList(ListNode* head) {
// 归并排序
if (!head || !head->next) return head;
ListNode *slow = head, *fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode *mid = slow->next;//注意1
slow->next = nullptr;
ListNode *left = sortInList(head);
ListNode *right = sortInList(mid);
return merge(left, right);
}
ListNode *merge(ListNode* h1, ListNode* h2){
ListNode dummy(0),*p = &dummy;
while(h1 && h2){
if(h1->val < h2->val){
p->next=h1;
h1=h1->next;
}else{
p->next=h2;
h2=h2->next;
}
p=p->next;//注意2
}
if(h1) p->next=h1;
if(h2) p->next=h2;
return dummy.next;
}
};
快速排序
找基准:遍历后面的结点:比基准小的放左边,大的放右边;然后合并左边+基准+右边
// 快速排序函数
ListNode* quickSortList(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
// 选择一个基准元素
ListNode *pivot = head;
head = head->next;
pivot->next = NULL;
// 将链表分成两个部分
ListNode *left = NULL, *right = NULL;
while (head != NULL) {
ListNode *next = head->next;
if (head->val < pivot->val) {
head->next = left;
left = head;
} else {
head->next = right;
right = head;
}
head = next;
}
// 递归地对左右两个部分进行排序
left = quickSortList(left);
right = quickSortList(right);
// 将左右两个部分和基准元素拼接起来
if (left == NULL) {
pivot->next = right;
return pivot;
} else {
ListNode *tail = left;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = pivot;
pivot->next = right;
return left;
}
}