Leetcode刷题之链表题

链表

TIPS

  • 双指针
  • 创建一个(-1)节点指向给定头节点
  • 维护头节点

1. 链表基础

1.1 反转链表(Leetcode 206, Easy)

反转一个单链表。示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

代码

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* ans=NULL;
        ListNode* current=head;
        ListNode* pnext=NULL;
        
        while(current!=NULL){
            pnext=current->next;
            current->next=ans;
            ans=current;
            current=pnext;
        }
        return ans;
    }
};

1.2 倒数第k个节点(剑指offer 22, Easy)

输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。例如,一个链表有6个节点,从头节点开始,它们的值依次是1、2、3、4、5、6。这个链表的倒数第3个节点是值为4的节点。

给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.

class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        ListNode* first=head;
        ListNode* second=head;
        int size=0;
        while(first!=NULL){
            size++;
            first=first->next;
        }
        first=head;
        if(k>size)
            return NULL;
        if(k==size)
            return head;
        while(k){
            first=first->next;
            k--;
        }
        while(first!=NULL &&second!=NULL){
            first=first->next;
            second=second->next;
        }
        return second;

    }
};

ListNode* getKthFromEnd(ListNode* head, int k) {
        if(head==NULL)
            return NULL;

        ListNode* first=head;
        ListNode* second=head;

        while(k>1){
            first=first->next;
            k--;
        }
        while(first->next!=NULL){
            first=first->next;
            second=second->next;
        }
        return second;
    }

1.3 链表的中间结点(Leetcode 876, Easy)

给定一个头结点为 head 的非空单链表,返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

示例 1:
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。

ListNode* middleNode(ListNode* head) {
        if(head->next==NULL)
            return head;

        ListNode* fast=head;
        ListNode* slow=head;
        while(fast!=NULL&&fast->next!=NULL){
            fast=fast->next;
            fast=fast->next;
            slow=slow->next;
        }
        return slow;
    }

1.4 两数相加(Leetcode2, Medium)

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
示例 2:
输入:l1 = [0], l2 = [0]
输出:[0]
示例 3:
输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *current1=l1, *current2=l2;
        ListNode *temp;
        int len1=0, len2=0;
        int add=0;
        while(current1!=NULL){
            len1++;
            current1=current1->next;
        }
        while(current2!=NULL){
            len2++;
            current2=current2->next;
    }
    current1=l1;
    current2=l2;
    if(len1<len2){
        temp=current1;
        current1=current2;
        current2=temp;
    }
    ListNode* ans=current1;
    while(current1!=NULL){
        if(current2!=NULL){
            current1->val=current1->val+current2->val;
        }
        if(add==1){
            current1->val++;
            add=0;
        }
        if(current1->val>=10){
            if(current1->next==NULL){
                ListNode* extra=new ListNode(0);
                current1->next=extra;
            }
            add=1;
            current1->val-=10;
        }
        current1=current1->next;
        if(current2!=NULL)
            current2=current2->next;
    }
    return ans;
    }

1.5 相交链表(Leetcode160, Easy)

编写一个程序,找到两个单链表相交的起始节点。

int get_list_length(ListNode* head){
    int length=0;
    while(head){
        length++;
        head=head->next;
    }
    return length;
}

ListNode* forward_long_list(int long_len, int short_len, ListNode* head){
    int difference=long_len - short_len;
    while(difference){
        head=head->next;
        difference--;
    }
    return head;

}

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int len_A=get_list_length(headA);
        int len_B=get_list_length(headB);
        if(len_A>len_B){
            headA=forward_long_list(len_A, len_B,headA);
        }else{
            headB=forward_long_list(len_B, len_A,headB);
        }
        while(headA && headB){

            if(headA==headB){
                return headA;
            }
            headA=headA->next;
            headB=headB->next;
        }
        return NULL;

    }
};

2. 环形链表

2.1 环形链表 II(Leetcode 142, Medium)

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。

说明:不允许修改给定的链表。

进阶:

你是否可以使用 O(1) 空间解决此题?

ListNode *detectCycle(ListNode *head) {
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast&&slow){
            fast=fast->next;
            fast=fast->next;
            slow=slow->next;
            if(fast==slow){
                fast=head;
                while(fast!=slow){
                    fast=fast->next;
                    slow=slow->next;
                }
                return fast;
            }
        }
        return NULL;
        
    }

2.2 旋转链表(Leetcode61, Medium)

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:
输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:
输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL

ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL)
            return NULL;
        int len=0;
        ListNode* current=head;

        ListNode* second=new ListNode(-1);
        ListNode* phead=second;
        
        ListNode* ans;


        while(current!=NULL){
            len++;
            ListNode* node=new ListNode(current->val);
            second->next=node;
            second=second->next;
            if(current->next==NULL){
                current->next=phead->next;
                break;
            }
            current=current->next;

            
        }

        current=head;
        int extra=k%len;

        for(int i=1;i<=2*len;i++){
            if(i==len-extra+1)
                ans=current;
            if(i==2*len-extra){
                current->next=NULL;
                break;
            }
            current=current->next;
            
        }
        return ans;

    }

3. 有序链表

3.1 去除有序链表的重复元素(Leetcode 83, Easy)

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL)
            return NULL;

        ListNode *current=head->next;
        ListNode* pre=head;

        while(current!=NULL){
            if(current->val==pre->val){
                pre->next=current->next;
            }else{
                pre=current;
            }
            current=current->next;

        }
        return head;
    
        }
    };

3.2 合并有序链表(Leetcode 21, Easy)

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

if(l1==NULL&&l2==NULL){
            return NULL;
        }else{
            if(l1==NULL){
                return l2;
            }
            if(l2==NULL){
                return l1;
            }
        }
        
        ListNode* ans_head=new ListNode(-1);
        ListNode* ans=ans_head;

        ListNode *current1=l1, *current2=l2;

        while(current1!=NULL && current2!=NULL){
            if(current1->val<current2->val){
                ans->next=current1;
                current1=current1->next;
            }else{
                ans->next=current2;
                current2=current2->next;
            }
            ans=ans->next;
        }
        if(current1==NULL){
            ans->next=current2;
        }
        if(current2==NULL){
            ans->next=current1;
        }
        return ans_head->next;

4. 删除链表

  • 考虑删除一个或多个尾节点
  • 考虑删除一个或多个头节点
  • 考虑删除完节点链表变空链表情况

4.1 删除链表中的节点(Leetcode237, Easy)

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点 。

示例 1:
输入:head = [4,5,1,9], node = 5
输出:[4,1,9]
解释:给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入:head = [4,5,1,9], node = 1
输出:[4,5,9]
解释:给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

提示:

链表至少包含两个节点。
链表中所有节点的值都是唯一的。
给定的节点为非末尾节点并且一定是链表中的一个有效节点。
不要从你的函数中返回任何结果。

class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val=node->next->val;
        node->next=node->next->next;
    }
};

4.2 删除链表的倒数第 N 个结点(Leetcode19, Medium)

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

进阶:你能尝试使用一趟扫描实现吗?

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1
输出:[]
示例 3:
输入:head = [1,2], n = 1
输出:[1]

ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head==NULL)
            return NULL;

        ListNode* pre_head=new ListNode(-1);
        pre_head->next=head;
        ListNode* first=pre_head;
        ListNode* second=pre_head;

        while(n){
            first=first->next;
            n--;
        }
        while(first->next!=NULL){
            first=first->next;
            if(first->next==NULL){
            }
            second=second->next;
        }
        second->next=second->next->next;
        return pre_head->next;

4.3 移除链表元素(Leetcode203, Easy)

删除链表中等于给定值 val 的所有节点。

示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

/*
*设置头节点pre_head,使之next指向head
*返回pre_head->next
*/
ListNode* removeElements(ListNode* head, int val) {
        if(head==NULL)
            return NULL;
        if(head->next==NULL && head->val==val)
            return NULL;
        ListNode* pre_head=new ListNode(-1);
        pre_head->next=head;
        ListNode* pre=pre_head;
        while(pre_head->next!=NULL){
            if(pre_head->next->val==val){
                pre_head->next=pre_head->next->next;
            }else{
                pre_head=pre_head->next;
            }            
        }

        return pre->next;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值