leetcode刷题之链表

合并两个有序链表

在这里插入图片描述

参考代码
普通解法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* newHead=new ListNode(0);
        ListNode* pre=newHead;
        while(l1&&l2){
            if(l1->val<=l2->val){
                pre->next=l1;
                pre=l1;
                l1=l1->next;
            }else{
                pre->next=l2;
                pre=l2;
                l2=l2->next;
            }
        }
        if(l1) pre->next=l1;
        if(l2) pre->next=l2;
        return newHead->next;
    }
};
递归解法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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;
        }
    }
};
删除链表的倒数第N个节点(LeetCode.17)

在这里插入图片描述

参考代码
递归解法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int count=0;
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head==NULL){
            return NULL;
        }
        head->next=removeNthFromEnd(head->next,n);
        count++;
        if(count==n){
            return head->next;
        }else{
            return head;
        }
    }
};
双指针解法
/**
 * 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* tempNode=new ListNode(0);
        tempNode->next=head;
        ListNode* p=tempNode;
        ListNode* q=tempNode;
        for(int i=0;i<n+1;i++)
            q=q->next;
        while(q){
            p=p->next;
            q=q->next;
        }
        
        ListNode* delNode=p->next;
        p->next=delNode->next;
        delete delNode;
        
        ListNode* retNode=tempNode->next;
        delete tempNode;
        
        return retNode;
    }
};
环形链表

在这里插入图片描述

参考代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head==NULL) return false;
        ListNode* p=head;
        ListNode* q=head->next;
        while(p!=q){
            if(p==NULL||q==NULL||q->next==NULL)
                return false;
            p=p->next;//慢指针
            q=q->next->next;//快指针
        }
        return true;
    }
};
回文链表

在这里插入图片描述

参考代码
/**
 * 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* fast=head->next;
        ListNode* slow=head;
        ListNode* q=NULL,*p;
        //找到中点,并反转前半部分链表
        while(fast&&fast->next){
            fast=fast->next->next;
            p=slow->next;
            slow->next=q;
            q=slow;
            slow=p;
        }
        //将slow的next指针反转
        p=slow->next;
        slow->next=q;
        //如果是链表长度为奇数
        q=(fast==NULL?slow->next:slow);//若是奇数则fast指向NULL
        //判断是否是回文串
        while(p){
            if(q->val!=p->val) return false;
            q=q->next;
            p=p->next;
        }
        return true;
    }
};
旋转链表

在这里插入图片描述

参考代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL||head->next==NULL||k==0) return head;
        //先将链表连成环,同时计算链表的长度
        ListNode* tail=head;
        int n=1;
        while(tail->next){
            tail=tail->next;
            n++;
        }
        tail->next=head;
        //找到新的链表尾和链表头
        //新的链表尾在第n-k%n-1个位置
        ListNode* newTail=head;
        for(int i=0;i<n-k%n-1;i++){
            newTail=newTail->next;
        }
        ListNode* newHead=newTail->next;
        newTail->next=NULL;
        return newHead;
    }
};
删除排序链表中的重复元素

在这里插入图片描述

参考代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool isLeagel(ListNode* pre,ListNode* cur,ListNode* next){
        if((pre&&pre->val==cur->val)||(next&&next->val==cur->val))
            return false;
        return true;
    }
    ListNode* deleteHelper(ListNode* pre,ListNode* head){
        if(head==NULL){
            return NULL;
        }
        ListNode* nextNode=head->next;
        if(isLeagel(pre,head,nextNode)){
            head->next=deleteHelper(head,nextNode);
            return head;
        }else{
            return deleteHelper(head,nextNode);
        }
    }
    ListNode* deleteDuplicates(ListNode* head) {
        return deleteHelper(NULL,head);
    }
};
有序链表转换二叉搜素树

在这里插入图片描述

参考代码
递归解法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(ListNode* head,ListNode* tail){
        if(head==tail) return NULL;
        //找到当前链表的中点
        ListNode* slow=head;
        ListNode* fast=head->next;
        while(fast&&fast!=tail&&fast->next&&fast->next!=tail){
            slow=slow->next;
            fast=fast->next->next;
        }
        TreeNode* newNode=new TreeNode(slow->val);
        newNode->left=buildTree(head,slow);
        newNode->right=buildTree(slow->next,tail);
        return newNode;
    }
    TreeNode* sortedListToBST(ListNode* head) {
        return buildTree(head,NULL);
    }
};

时间复杂度为O(nlogn),空间复杂度为O(logn)

空间换时间解法
//先将链表数据存储在数组中,从而取中间元素的时间复杂度降低为O(1),从而可以将时间复杂度降低为O(n),而空间复杂度升为(O(n))
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> a;
    void getA(ListNode* head){
        while(head){
            a.push_back(head->val);
            head=head->next;
        }
    }
    TreeNode* buildTree(int left,int right){
        if(left>right) return NULL;
        int mid=(left+right)/2;
        TreeNode* newNode=new TreeNode(a[mid]);
        if(left==right) return newNode;
        newNode->left=buildTree(left,mid-1);
        newNode->right=buildTree(mid+1,right);
        return newNode;
    }
    TreeNode* sortedListToBST(ListNode* head) {
        getA(head);
        return buildTree(0,a.size()-1);
    }
};
环形链表

在这里插入图片描述

参考代码

使用Floyd算法:
1.利用快慢指针找到交点(注意这里的快慢指针要相同),若无环则直接返回NULL;
2.定义原起点为p1,交点为p2,p1,p2同时向前移动,每次移动一步,则再次相交处就是环的入口

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(head==NULL) return NULL;
        ListNode* slow=head;
        ListNode* fast=head;
        do{
            if(fast->next==NULL||fast->next->next==NULL)
                return NULL;
            slow=slow->next;
            fast=fast->next->next;
        }while(slow!=fast);
        ListNode* p1=head;
        ListNode* p2=slow;
        while(p1!=p2){
            p1=p1->next;
            p2=p2->next;
        }
        return p1;
    }
};
对链表进行插入排序

在这里插入图片描述

参考代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        //为了方便移动链表,在比较的同时反转链表
        ListNode* q=NULL;
        ListNode* p=head;
        ListNode* nextNode=head;
        while(nextNode){
            nextNode=p->next;
            p->next=q;
            q=p;
            //比较,移动节点
            ListNode* pre=p->next;
            int tempVal=p->val;
            while(pre&&pre->val<tempVal){
                p->val=pre->val;
                p=pre;
                pre=pre->next;
            }
            p->val=tempVal;
            p=nextNode;
        }
        return q;
    }
};
排序链表

在这里插入图片描述

解题思路

在这里插入图片描述

参考代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    //将链表的前n个节点切割出去,返回下一段的第一个节点
    ListNode* cut(ListNode* head,int n){
        ListNode* p=head;
        while(--n&&p){
            p=p->next;
        }
        if(!p) return NULL;
        ListNode* nextNode=p->next;
        p->next=NULL;
        return nextNode;
    }
    //将两个链表合并
    ListNode* merge(ListNode* head1,ListNode* head2){
        ListNode* newHead=new ListNode(0);
        ListNode* p=newHead;
        while(head1&&head2){
            if(head1->val<head2->val){
                p->next=head1;
                p=head1;
                head1=head1->next;
            }else{
                p->next=head2;
                p=head2;
                head2=head2->next;
            }
        }
        if(head1) p->next=head1;
        if(head2) p->next=head2;
        return newHead->next;
    }
    ListNode* sortList(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        //计算链表长度
        int len=0;
        ListNode* p=head;
        while(p){
            len++;
            p=p->next;
        }
        ListNode* dummyHead=new ListNode(0);
        dummyHead->next=head;
        ListNode* tail=dummyHead;
        ListNode* cur=head,*left,*right;
        for(int step=1;step<len;step*=2){
            while(cur){
                left=cur;
                right=cut(left,step);
                cur=cut(right,step);
                tail->next=merge(left,right);
                while(tail->next) tail=tail->next;
            }
            cur=dummyHead->next;
            tail=dummyHead;
        }
        return dummyHead->next;
    }
};

熟记cut(node,n)的写法

总结

1.快慢指针:

  • 删除倒数第几个节点
  • 判断链表中是否有环
  • 找中点

2.递归

  • 通常用于对链表的遍历,比如删除满足特定条件的节点

3.链表的反转,链表的cut(node,n)操作,两个链表的合并等操作要熟记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值