代码随想录算法训练营第四天| 24. 两两交换链表中的节点,19.删除链表的倒数第N个节点,面试题 02.07. 链表相交,142.环形链表II

36 阅读2分钟

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

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

文章讲解

视频讲解

难点:1. 理解交换链表后是如何连接的 2. 循环终止条件

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

可以优化卡哥的算法,将2.3的交换顺序调换,可以省去temp1变量指针。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy_head = ListNode(next=head)

        cur = dummy_head

        while cur.next and cur.next.next:
            
            temp = cur.next

            cur.next = cur.next.next
            temp.next = cur.next.next
            cur.next.next = temp
 
            cur = cur.next.next

        return dummy_head.next
       

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

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

文章讲解

视频讲解

重点:双指针fastslow多走N+1步,因为要删除倒数第N个节点需要知道倒数第N+1个节点。


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        
        dummy_head = ListNode(0,head)

        slow = fast = dummy_head

        for i in range(n+1):
            fast = fast.next

        while fast:
            slow = slow.next
            fast = fast.next
        
        slow.next = slow.next.next

        return dummy_head.next

02.07.链表相交

02.07. 链表相交

文章讲解

重点:链表元素末尾对齐

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        
        lenA = self.getLength(headA)
        lenB = self.getLength(headB)

        if lenA > lenB:
            headA = self.moveForward(headA, lenA-lenB)
        else:
            headB = self.moveForward(headB, lenB-lenA)

        while headA and headB:
            if headA == headB:
                return headA
            headA = headA.next
            headB = headB.next

        return None

    def getLength(self, head: ListNode) -> int:
        length = 0
        while head:
            head = head.next
            length += 1
        return length

    def moveForward(self, head: ListNode, steps:int) -> int:
        while steps:
            head = head.next
            steps -= 1
        return head
       

142.环形链表II

142.环形链表II

文章讲解

视频讲解

重点:找到环入口

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = head
        fast = head

        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next

        # 判断是否有环
            if fast == slow:
                index1 = fast
                index2 = head
                
                while index1 != index2:
                    index1 = index1.next
                    index2 = index2.next
                
                return index1
            
        return None