备战秋招之刷题篇 线性表之单链表

两数相加(medium)

在这里插入图片描述

/**
 * 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* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head=new ListNode(-1);
        ListNode* h=head;
        int sum=0;
        bool carry=false;
        while(l1 != nullptr || l2 != nullptr)
        {
            sum=0;
            if(l1 != nullptr)
            {
                sum+=l1->val;
                l1=l1->next;
            }
            if(l2 != nullptr)
            {
                sum+=l2->val;
                l2=l2->next;
            }
            if(carry)
                sum++;
            carry=(sum/10)==1?true:false;
            h->next=new ListNode(sum%10);
            h=h->next;
        }
        if(carry)
        {
            h->next=new ListNode(1);
        }
        return head->next;
    }
};

反转链表II(medium)

在这里插入图片描述
分析题目:其实和反转链表差不多,只是要多记录一些结点,比如头结点,left前一个结点,right后一个结点

/**
 * 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* reverseBetween(ListNode* head, int left, int right) {
        ListNode* dummy=new ListNode(0,head);
        ListNode* cur=dummy;
        ListNode* pre=head;
        ListNode* back=head;
        ListNode* fore=dummy;
        if(left==right)
        {
            return head;
        }
        else
        {
            for(int i=0;i<left;i++)
            {
                cur=cur->next;
                pre=pre->next;
                //fore=fore->next;
            }
            //cur=cur->next;
           // pre=pre->next;
            ListNode* p=cur;
            for(int i=0;i<left-1;i++)
            {
                fore=fore->next;
            }
            for(int j=0;j<right;j++)
            {
                back=back->next;
            }
            while(pre!=back)
            {
                ListNode* t=pre->next;
                pre->next=cur;
                cur=pre;
                pre=t;
            }
            fore->next=cur;
            p->next=back;
            return dummy->next;
        }
    }
};

分隔链表(medium)

在这里插入图片描述
直观来说我们只需维护两个链表 small 和 large 即可,small 链表按顺序存储所有小于 x 的节点,large 链表按顺序存储所有大于等于 x 的节点。遍历完原链表后,我们只要将 small 链表尾节点指向 large 链表的头节点即能完成对链表的分隔。
为了实现上述思路,我们设smallHead 和 \largeHead 分别为两个链表的哑节点,即它们的 next 指针指向链表的头节点,这样做的目的是为了更方便地处理头节点为空的边界条件。同时设small 和 large 节点指向当前链表的末尾节点。开始时smallHead=small, largeHead=large。随后,从前往后遍历链表,判断当前链表的节点值是否小于 x,如果小于就将small 的 next 指针指向该节点,否则将large 的 next 指针指向该节点。
遍历结束后,我们将large 的 next 指针置空,这是因为当前节点复用的是原链表的节点,而其 next 指针可能指向一个小于 x 的节点,我们需要切断这个引用。同时将small 的 next 指针指向 largeHead 的 next 指针指向的节点,即真正意义上的 large 链表的头节点。最后返回smallHead 的next 指针即为我们要求的答案。
时间复杂度:O(n)
空间复杂度:O(1)

/**
 * 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* partition(ListNode* head, int x) {
        ListNode* small=new ListNode(0);
        ListNode* large=new ListNode(0);
        ListNode* smallhead=small;
        ListNode* largehead=large;
        while(head != nullptr)
        {
            if(head->val <x)
            {
                small->next=head;
                small=small->next;
            }
            else
            {
                large->next=head;
                large=large->next;
            }
            head=head->next;
        }
        large->next=nullptr;
        small->next=largehead->next;
        return smallhead->next;
    }
};

删除排序链表中的重复元素(easy)

在这里插入图片描述
和上一道题思路差不多,这题我一次性过嘿嘿

/**
 * 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* deleteDuplicates(ListNode* head) {
        ListNode* ans=new ListNode(-200);
        ListNode* anshead=ans;
        while(head != nullptr)
        {
            if(ans->val!=head->val)
            {
                ans->next=head;
                ans=ans->next;
                head=head->next;
            }
            else
            {
                head=head->next;
            }
        }
        ans->next=nullptr;
        return anshead->next;
    }
};

我这个方法不是最佳的,最佳的是原地删除

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head) {
            return head;
        }

        ListNode* cur = head;
        while (cur->next) {
            if (cur->val == cur->next->val) {
                cur->next = cur->next->next;
            }
            else {
                cur = cur->next;
            }
        }

        return head;
    }
};

删除排序链表中的重复元素II(medium)

在这里插入图片描述
时间复杂度O(n),空间复杂度O(1)

/**
 * 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* deleteDuplicates(ListNode* head) {
        ListNode* dummy=new ListNode(-200,head);
        ListNode* p=dummy;
        while(p->next && p->next->next)
        {
            if(p->next->val != p->next->next->val)
            {
                p=p->next;
            }
            else
            {
                int x=p->next->val;
                while(p->next && p->next->val==x)
                {
                    p->next=p->next->next;
                }
            }
        }
        return dummy->next;
    }
};

旋转链表(medium)

在这里插入图片描述
在这里插入图片描述

/**
 * 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* rotateRight(ListNode* head, int k) {
        if(k==0 || head==nullptr || head->next==nullptr)
        {
            return head;
        }
        int n=1;
        ListNode* p=head;
        while(p->next != nullptr)
        {
            p=p->next;
            n++;
        }
        int a=n-k%n;
        if(a==n)return head;
        p->next=head;
        while(a--)
        {
            p=p->next;
        }
        head=p->next;
        p->next=nullptr;
        return head;
    }
};

删除倒数第N个结点(medium)

在这里插入图片描述
用快慢指针做

/**
 * 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) {
        ListNode* dummy=new ListNode(0,head);
        ListNode* fast=dummy;
        ListNode* slow=dummy;
        for(int i=0;i<n;i++)
        {
            fast=fast->next;
        }
        while(fast->next != nullptr)
        {
            fast=fast->next;
            slow=slow->next;
        }
        slow->next=slow->next->next;
        return dummy->next;
    }
};

两两交换链表中的结点(medium)

在这里插入图片描述
分析题目:其实和反转链表的思想差不多,只不过是两个两个的反转

/**
 * 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* swapPairs(ListNode* head) {
        ListNode* dummy=new ListNode(0,head);
        ListNode* pre=dummy;
        ListNode* cur=head;
        if(head==nullptr || head->next==nullptr)
        {
            return head;
        }
        while(cur != nullptr && cur->next != nullptr)//考虑个数为奇数的情况
        {
            pre->next=cur->next;
            pre=pre->next;
            ListNode* p=cur->next->next;
            pre->next=cur;
            cur->next=p;
            pre=pre->next;
            cur=cur->next;
        }
        return dummy->next;
        
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值