Leetcode 算法题 打卡day4

Leetcode 算法题 打卡day4

两两交换链表中的节点

leetcode 24 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
示例:
输入:head = [1,2,3,4]
输出:[2,1,4,3]

递归思路 递归的终止条件使链表中没有节点此时返回None,或者链表中只包含一个节点此时返回该节点head

# 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]:
        # 使用递归进行求解
        if not head or not head.next:
            return head
        node = self.swapPairs(head.next.next)
        cur = head.next
        cur.next = head
        head.next = node
        return cur 

迭代思路 迭代的思路 首先初始化一个虚拟头节点dum,之后使用一个tmp指针从dum开始遍历,每次遍历都去处理dum.nextdum.next.next两个节点。

class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # 使用递归进行求解
        dum = ListNode(-1)
        dum.next = head
        cur = dum
        while(cur.next and cur.next.next):
            node1 = cur.next 
            node2 = cur.next.next 
            cur.next = node2
            node1.next = node2.next # 注意这两句的顺序 要先将node2.next赋值给node1.next再改变node2.next
            node2.next = node1
            cur = node1 # 注意此处要将指针移动到node1的位置 而不是cur = cur.next
        return dum.next 

删除链表倒数第N个结点

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

思路 在一个链表中倒数第n个结点,同时是正数第N-n+1个节点。使用两个指针进行计数,fast指针移动n个结点,之后fast移动到末尾的步数,刚好是slow指针移动到N-n的位置的步数,要求slow指针停留到待删除节点的前一个方便删除。

# 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]:
        # 倒数第n个结点 正数第N-n+1个节点
        # 使用快慢指针 fast指针先出发n-1
        dum = ListNode(-1)
        dum.next = head
        fast = slow = dum
        for i in range(n):
            fast = fast.next
        while(fast.next):
            slow = slow.next
            fast = fast.next
        slow.next = slow.next.next
        return dum.next

链表相交

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

思路 可以将判断链表是否相交转换为链表是否存在环的问题,具体做法将A链表的末尾和B链表的head相连,之后使用龟兔赛跑快慢指针的方法,判断是否存在环,若有环代表相交。

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        if not headA or not headB:
            return None
        cur = headA
        while(cur and cur.next):
            cur = cur.next
        cur.next = headB
        # ""环""
        slow = fast = headA
        while(slow and fast and fast.next):
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                slow = headA
                while(slow != fast):
                    slow = slow.next
                    fast = fast.next
                cur.next = None
                return fast
        cur.next = None

环形链表II

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

为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

说明:不允许修改给定的链表。

思路 # TODO:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值