Day 4 链表: 24. 两两交换 ,19. 删除倒N ,02.07. 链表相交,142. 环形链表 II

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

  • 思路
    • example
    • dummyhead
    • 迭代

遍历cur(从dummyhead)开始,当cur.next and cur.next.next都非空时,处理后继两个节点 node1, node2。处理完移动cur到node1 (cur.next.next),继续。。。

  • 复杂度. 时间:O(n), 空间: O(1)
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummyhead = ListNode(-1, head)
        cur = dummyhead  
        while cur.next and cur.next.next:
            node1, node2 = cur.next, cur.next.next  
            cur.next = node2  
            node1.next = node2.next  
            node2.next = node1  
            cur = node1  
        return dummyhead.next  
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummyhead = ListNode(-1, head)
        pre, cur = dummyhead, head 
        while cur and cur.next:
            temp = cur.next.next 
            pre.next = cur.next 
            cur.next = temp 
            pre.next.next = cur 
            pre = cur 
            cur = temp 
        return dummyhead.next 
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummyhead = ListNode(-1, head) 
        pre, cur = dummyhead, head   
        while cur and cur.next:
            temp = cur.next.next   
            pre.next = cur.next   
            cur.next.next = cur        
            cur.next = temp  
            pre = cur     
            cur = temp     
        return dummyhead.next   
  • 递归
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head == None or head.next == None:
            return head  
        head2 = self.swapPairs(head.next.next)
        node1, node2 = head, head.next  
        node1.next = head2  
        node2.next = node1  
        return node2  
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head == None or head.next == None:
            return head 
        second = head.next 
        third = head.next.next 
        second.next = head 
        head.next = self.swapPairs(third)
        return second 

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

  • 思路
    • example
    • dummyhead: 方便删除链表头节点。
    • Two Pointer: one-pass
      • 初始化 slow, fast = dummyhead, head
      • ’fast - slow = n+1‘, slow与fast中间间隔n个节点
        • when fast == None, then slow.next is nth element from the end
  • 复杂度. 时间:O(n), 空间: O(1)
# slow, fast 相同起点
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummyhead = ListNode(-1, head)
        slow, fast = dummyhead, dummyhead 
        for _ in range(n):
            fast = fast.next 
        while fast.next:
            slow = slow.next 
            fast = fast.next  
        slow.next = slow.next.next 
        return dummyhead.next      
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummyhead = ListNode(-1, head) 
        slow, fast = dummyhead, dummyhead  
        for _ in range(n+1):
            fast = fast.next   
        while fast:
            slow = slow.next 
            fast = fast.next  
        slow.next = slow.next.next  
        return dummyhead.next  

面试题 02.07. 链表相交

160. Intersection of Two Linked Lists


  • 思路
    • example
    • 假设无环
    • 重点:空间O(1),否则简单hash技术解决。
    • 如果有相交点,则有共同tail.
    • 1。 反转两个链表(in-place),顺序比较,最后需要再反转复原。----- 有相交点时,反转其中一个会破坏另一个结构。
    • 2。 ListA 末尾指向 ListB开头,如果有相交点,从A出发遍历会碰到环,转化为环形链表找相交点问题。
    • 3。简单方法: 计算lenA, lenB; 假设lenA < lenB,先在B遍历(lenB - lenA)个节点, 然后A,B同时遍历,依次比较。
  • 复杂度. 时间:O(m+n), 空间: O(1)
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
        curA, curB = headA, headB
        if lenA > lenB:
            curA, curB = curB, curA
            lenA, lenB = lenB, lenA 
        for _ in range(lenB - lenA):
            curB = curB.next 
        while curA:
            if curA == curB:
                return curA
            else:
                curA = curA.next 
                curB = curB.next
        return None 
  • "手中无环,心中有环"
    • 同时从A,B起点出发(假设lenA < lenB)
      • 当A到达末尾时,连接到B的头部
      • 当B到达末尾时,连接到A的头部
      • curA, curB最后会在相交点(可能是None) 相遇。(“双环”)
      • None节点必须要遍历!
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        curA, curB = headA, headB
        while curA != curB:
            if curA != None:
                curA = curA.next 
            else:
                curA = headB
            if curB != None:
                curB = curB.next  
            else:
                curB = headA
        return curA  
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        cur1, cur2 = headA, headB  
        while cur1 != cur2:
            if cur1:
                cur1 = cur1.next  
            else:
                cur1 = headB 
            if cur2:
                cur2 = cur2.next 
            else:
                cur2 = headA  
        return cur1  
  • 另一种思路


142. 环形链表 II

  • 思路
    • example

    • 关键:空间

    • 第一步:用快慢针判断是否有环

      • 快针走2步
      • 慢针走1步
    • 第二步:找到环入口

      • fast = head
      • 然后slow, fast等速移动,在入口相遇
        • m: 不在环内的节点数
        • t: 第一次相遇时,慢针在环内走的步数
        • k: 环内节点个数
        • 快针走的距离 = 2 * 慢针走的距离
        • ( 整数)


    • 不使用dummyhead

      • 细节:循环:while fast and fast.next: (边界情况:节点数为0或1)
  • 复杂度. 时间:O(n), 空间: O(1)
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow, fast = head, head 
        while fast and fast.next:
            slow = slow.next 
            fast = fast.next.next 
            if  slow == fast:
                break 
        if fast == None or fast.next == None:
            return None 
        fast = head 
        while slow != fast:
            slow = slow.next 
            fast = fast.next 
        return slow 
  • 使用dummyhead (本质一样)
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: 
        dummyhead = ListNode(0, head)
        slow, fast = dummyhead, dummyhead  
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                break
        if fast == None or fast.next == None:
            return None
        fast = dummyhead 
        while slow != fast:
            slow = slow.next
            fast = fast.next
        return slow
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow, fast = head, head  
        while fast and fast.next:
            slow = slow.next 
            fast = fast.next.next  
            if slow == fast:
                break  
        if fast == None or fast.next == None:
            return None   
        fast = head  
        while slow != fast:
            slow = slow.next  
            fast = fast.next  
        return slow  

你可能感兴趣的:(Day 4 链表: 24. 两两交换 ,19. 删除倒N ,02.07. 链表相交,142. 环形链表 II)