代码随想录算法训练营第四天| 24. 两两交换链表中的节点 、160.链表相交、142.环形链表II

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

题目链接:24. 两两交换链表中的节点

C++:

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode *dummyhead = new ListNode(0, head);
        ListNode *cur = dummyhead;
        while(cur->next != nullptr && cur->next->next != nullptr)
        {
            ListNode *temp = cur->next;
            cur->next = temp->next;
            temp->next = cur->next->next;
            cur->next->next = temp;
            cur = temp;
        }

        return dummyhead->next;
    }
};

Python:

class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummyhead = ListNode(0, head)
        cur = dummyhead
        while cur.next != None and cur.next.next != None:
            temp = cur.next
            cur.next = temp.next
            temp.next = cur.next.next
            cur.next.next = temp
            cur = temp
        return dummyhead.next

Leetcode19.删除链表的倒数第N个节点

题目链接:19.删除链表的倒数第N个节点

C++:(双指针)

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummyhead = new ListNode(0, head);
        ListNode *fast = dummyhead;
        ListNode *slow = dummyhead;
        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 dummyhead->next;
    }
};

Python:

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy_head = ListNode(0, head)
        fast = dummy_head
        slow = dummy_head
        while(fast and n):
            fast = fast.next
            n -= 1
        while(fast.next):
            fast = fast.next
            slow = slow.next
        slow.next = slow.next.next
        return dummy_head.next

面试题 02.07. 链表相交

题目链接:面试题 02.07. 链表相交

C++:(暴力解法)

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        // ListNode *curA = headA;
        ListNode *curB = headB;
        ListNode *curA = headA;
        // if(curA == NULL || curB == NULL)
        //     return NULL;
        while(curB != NULL)
        {
            ListNode *curA = headA;
            while(curA != NULL)
            {
                if(curA != curB)
                    curA = curA->next;
                else
                    break;
            }
            if(curA != curB)
                curB = curB->next;
            else
                return curA;
        }
        return NULL;
    }
};

Python:(求长度)

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        lenA, lenB = 0, 0
        cur = headA
        while cur:
            cur = cur.next
            lenA += 1
        cur =headB
        while cur:
            cur = cur.next
            lenB += 1
        
        if lenA > lenB:    # 使B成为长链表
            lenA, lenB = lenB, lenA
            headA, headB = headB, headA
        
        curA, curB = headA, headB
        for _ in range(lenB - lenA):
            curB = curB.next

        while curA:
            if(curA == curB):
                return curA
            else:
                curA = curA.next
                curB = curB.next
        
        return None

Leetcode142.环形链表II

题目链接:142.环形链表II

注意:

慢指针行走距离:x+y

快指针行走距离:x+y+n*(y+z)

即:2*(x+y) = x+y+n*(y+z)

              x+y=n*(y+z)

                  x= n*(y+z)-y

                  x= (n-1)*(y+z)+z

C++:

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast = head;
        ListNode *slow = head;
        while(fast != NULL && fast->next != NULL)
        {
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow)
            {
                ListNode *index1 = slow;
                ListNode *index2 = head;
                while(index1 != index2)
                {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2;
            }
        }
        return NULL;
    }
};

Python:

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        fast, slow = head, head
        while fast != None and fast.next != None:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                index1 = slow
                index2 = head
                while index1 != index2:
                    index1 = index1.next
                    index2 = index2.next
                return index2
        return None

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值