Day04 链表

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

解法:首先需要明确处理中涉及到三个节点,left,cur,right。其中cur和right是我们要交换的,但cur的前一个节点的指向也得更新,所以涉及到三个。设置一个虚拟节点可以方便头节点的交换。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy_head = ListNode(next = head)
        cur1 = dummy_head
        while cur1.next and cur1.next.next:
            cur2 = cur1.next
            cur3 = cur1.next.next

            cur2.next = cur3.next
            cur3.next = cur2
            cur1.next = cur3

            cur1 = cur1.next.next
        return dummy_head.next    

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

解法:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummy_head = ListNode(next = head)
        slow = dummy_head
        fast = head
        for _ in range(n):
            fast = fast.next
        while fast != None:
            slow = slow.next
            fast = fast.next
        
        slow.next = slow.next.next

        return dummy_head.next

面试题 02.07. 链表相交

解法一:让A和B尾部对齐,然后让长的一方移动head,直到与短的一方head对齐,然后比较两个head,并不断移动至结尾。

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

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        if headA == None or headB == None:
            return None
        lenA = 0
        curA = headA
        while curA != None:
            curA = curA.next
            lenA += 1
        
        lenB = 0
        curB = headB
        while curB != None:
            curB = curB.next
            lenB += 1
        
        if lenA > lenB:
            spa = lenA - lenB
            for _ in range(spa):
                headA = headA.next
        else:
            spa = lenB - lenA
            for _ in range(spa):
                headB = headB.next
        while headA and headB:
            if headB and headB == headA:
                return headB
            headB = headB.next
            headA = headA.next
        return None

解法二:代码随想录里的思路,自己没想到这种解法。感觉也是类似于让他们分别走到距离结尾相同长度的位置,但是处理方法不同,感觉过于巧妙了想不到哈哈

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        """
        根据快慢法则,走的快的一定会追上走得慢的。
        在这道题里,有的链表短,他走完了就去走另一条链表,我们可以理解为走的快的指针。

        那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个
        位置相遇
        """
        if headA is None or headB is None: 
            return None
        cur_a, cur_b = headA, headB     # 用两个指针代替a和b


        while cur_a != cur_b:
            cur_a = cur_a.next if cur_a else headB      # 如果a走完了,那么就切换到b走
            cur_b = cur_b.next if cur_b else headA      # 同理,b走完了就切换到a

        return cur_a

142. 环形链表 II

解法:先判断是否有环,若有环则算出环的长度,让fast先移动出一个环的长度,然后fast和slow一同移动,因为fast提前移动了一个环的长度了,所以到slow==fast的时候,就是环的入口了。

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

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return None
        slow = head
        fast = head.next
        # 找环 只有fast与slow相遇才会有环
        while fast and fast.next and fast != slow:
            slow = slow.next
            fast = fast.next.next
        # 无环 返回None
        if fast is None or fast.next is None:
            return None
        # 计算环的大小    
        cnt = 1
        node = fast.next
        while node != slow:
            node = node.next
            cnt += 1
        # 让fast提前走一个环的长度
        fast = head
        for _ in range(cnt):
            fast = fast.next
        # 因为fast比slow提前走了一个环,所以fast与slow相遇的位置就是入口
        slow = head
        while fast != slow:
            fast = fast.next
            slow = slow.next
        return fast           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值