leetcode 24. 两两交换链表中的节点、 19.删除链表的倒数第N个节点 、面试题 02.07. 链表相交 、 142.环形链表II

24. 两两交换链表中的节点

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

思路:

定义一个虚拟头指针题目会比较好理解,定义三个指针,一个虚拟头指针pre,一个指针cur,一个指针ret.整个链表可以写三步操作在一个循环里。

        istNode* ppp = ret->next;//临时结点

            pre->next = ret;

            ret->next = cur;

            cur->next = ppp;

时间复杂度:

O(n)

代码:
/**
 * 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) {
        if(head==NULL) return NULL;
        ListNode* pre = new ListNode(0);
        pre->next = head;
        ListNode* result = pre;
        ListNode* cur = head;
        ListNode*ret = cur->next;
        while(cur!=NULL&&cur->next!=NULL)
        {
            ListNode* ppp = ret->next;
            pre->next = ret;
            ret->next = cur;
            cur->next = ppp;
            if(cur->next!=NULL)
          {  pre = cur;
            cur = pre->next;
            ret = cur->next;
          }
        }
        return result->next;
    }
};

 19. 删除链表的倒数第 N 个结点

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

思路:

我们可以遍历一边链表,记录元素结点个数,第二次遍历进行删除结点。

时间复杂度:

O(0)

代码:
/**
 * 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) {
        if(head==NULL) return NULL;
        ListNode* pnode = new ListNode(0);
        pnode->next = head;
        ListNode* result = pnode;
        int count = 0;
        ListNode* pre = head;
        while(pre!=NULL)
        {
            count++;
            pre = pre->next;
        }
        int index = count-n;
        while(index--)
        {
            pnode = pnode->next;
        }
        ListNode* ret = pnode->next;
        pnode->next = ret->next;
        delete ret;
        return result->next;

    }
};

面试题 02.07. 链表相交

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

思路:

链表相交这里注意两个链表有相同的地址,我们可以分别求出两个链表的元素数量countA,countB.

然后从尾部进行对齐。继而进行判断curA,curB地址是否一致。

时间复杂度:

O(m+n)

代码:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA==NULL||headB==NULL) return NULL;
        ListNode* ha = headA;
        ListNode* hb = headB;
        int countA = 0;
        int countB = 0;
        while(ha!=NULL)
        {
            countA++;
            ha = ha->next;
        }
        while(hb!=NULL)
        {
            countB++;
            hb = hb->next;
        }
        ha = headA;
        hb = headB;
        if(countA<countB)
        {
            swap(countA,countB);
            swap(ha,hb);
        }
        int index = countA-countB;
        while(index--&&ha!=NULL)
        {
            ha = ha->next;
        }
        while(ha!=NULL)
        {
            if(ha!=hb)
            {   
                ha = ha->next;
                hb = hb->next;
            }
            else
            return ha;
        }
        return NULL;
        
    }
};

142. 环形链表 II

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

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改 链表。

思路:

首先要判断链表是否有环,我们可以定义快慢指针slow,fast.slow每次走一步,fast每次走两步,如果有环fast和slow肯定可以相遇。

其次我们判断有环之后怎么找出环的入口呢?

fast指针和slow指针相遇后,我们再定义一个指针index指向头结点head,然后让指针index和fast每次走一步,相遇即为链表环的入口。

大家可以看卡尔大佬的解释,在这附上连接代码随想录环形链表

时间复杂度:

O(n)

代码:
/**
 * 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;
        if(head->next==NULL) return NULL;
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast!=NULL&&fast->next!=NULL)
        {
            slow = slow->next;
            fast = fast->next->next;
            if(fast==slow)
            {
                ListNode* pre = head;
                 while(fast->next!=NULL&&pre!=fast)
                {
                    pre = pre->next;
                    fast = fast->next;
            
                }
                     if(pre==fast)
                     return pre;
            }
        }
        return NULL;
    }
};

 还有很多瑕疵,还需继续坚持!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我卡卡西cc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值