1.链表交点
https://leetcode.com/problems/intersection-of-two-linked-lists/description/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* p1 = headA;
ListNode* p2 = headB;
while(p1!=p2){
p1 = (p1==nullptr)?headB:p1->next;
p2 = (p2==nullptr)?headA:p2->next;
}
return p1;
}
找到A链表结尾后指针重新设置指向B链表头节点,找到B链表结尾后指针重新设置指向A链表开头,当两指针相等时,就找到了交点
2.翻转链表
https://leetcode.com/problems/reverse-linked-list/description/
递归法要简单
ListNode* reverseList(ListNode* head) {
if(head ==nullptr|| head->next == nullptr)
return head;
ListNode* newHead = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
3.归并两个有序的链表
同样递归法简单
https://leetcode.com/problems/merge-two-sorted-lists/description/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==nullptr) return l2;
if(l2==nullptr) return l1;
if(l1->val < l2->val){
l1->next = mergeTwoLists(l1->next,l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1,l2->next);
return l2;
}
}
4.有序链表中删除重复节点
https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
同样递归法简单
ListNode* deleteDuplicates(ListNode* head) {
if(head==nullptr || head->next==nullptr)
return head;
head->next = deleteDuplicates(head->next);
return head->val==head->next->val?head->next:head;
}
5.删除链表的倒数第 n 个节点
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
双指针思想
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(head==nullptr || head->next==nullptr)
return nullptr;
ListNode* p1=head;
ListNode* p2 = head;
for(int i=0;i<n-1;++i){
p2 = p2->next;
}
ListNode* p1Pre = nullptr;
while(p2->next){
p2 = p2->next;
p1Pre = p1;
p1 = p1->next;
}
if(p1Pre==nullptr){
return head->next;
}
p1Pre->next = p1->next;
p1->next = nullptr;
return head;
}
6.交换链表中的相邻结点
https://leetcode.com/problems/swap-nodes-in-pairs/description/
面试遇到过
ListNode* swapPairs(ListNode* head) {
if(head==nullptr || head->next==nullptr)
return head;
ListNode* p1 = head;
ListNode* p2 = head->next;
ListNode* pPre = new ListNode(0);
ListNode* newHead = pPre;
while(p1!=nullptr && p1->next!=nullptr){
p2 = p1->next;
p1->next = p2->next;
p2->next = p1;
pPre->next = p2;
pPre = p1;
p1 = p1->next;
}
return newHead->next;
}
7.链表求和
https://leetcode.com/problems/add-two-numbers-ii/description/
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* p1 = l1;
ListNode* p2 = l2;
int p1Num=0,p2Num=0;
vector<ListNode*> v1,v2;
while(p1){
v1.push_back(p1);
p1 = p1->next;
p1Num++;
}
while(p2){
v2.push_back(p2);
p2 = p2->next;
p2Num++;
}
ListNode* newHead = new ListNode(-1);
int carry=0;
int i=p1Num-1,j=p2Num-1;
for(;i>=0 && j>=0;--i,--j){
int sum = v1[i]->val + v2[j]->val + carry;
if(sum>=10){
sum = sum%10;
carry = 1;
}
else{
carry=0;
}
ListNode* node = new ListNode(sum);
node->next = newHead->next;
newHead->next = node;
}
if(i<0){
for(int n=j;n>=0;--n){
int sum = v2[n]->val + carry;
if(sum>=10){
sum = sum%10;
carry=1;
}
else{
carry=0;
}
ListNode* node = new ListNode(sum);
node->next = newHead->next;
newHead->next = node;
}
}
if(j<0){
for(int n=i;n>=0;--n){
int sum = v1[n]->val + carry;
if(sum>=10){
sum = sum%10;
carry=1;
}
else{
carry=0;
}
ListNode* node = new ListNode(sum);
node->next = newHead->next;
newHead->next = node;
}
}
if(carry==1){
ListNode* node = new ListNode(1);
node->next = newHead->next;
newHead->next = node;
}
return newHead->next;
}
8.回文链表
https://leetcode.com/problems/palindrome-linked-list/description/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==nullptr || head->next==nullptr)
return true;
ListNode* p = head;
int num=0;
while(p){
p = p->next;
num++;
}
p = head;
for(int i=0;i<num/2;++i){
p = p->next;
}
if(num%2!=0){
p = p->next;
}
ListNode* p2 = reverse(p);
p = head;
for(int i=0;i<num/2;++i){
if(p->val != p2->val)
return false;
p = p->next;
p2 = p2->next;
}
return true;
}
ListNode* reverse(ListNode* head){
if(head==nullptr || head->next==nullptr)
return head;
ListNode* newHead = reverse(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
};