算法题day5(补卡)

一、复习链表知识

二、刷链表题:

1.leetcode题目24 两两交换链表节点(medium):

题目描述:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

思路:①构建虚拟头节点,②画图操作,③需要保留节点,不然操作之后就找不到顺序了。

解决:

# 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_node = ListNode(val= 0,next = head)
        cur = dummy_node
        while cur.next and cur.next.next:
            next1 = cur.next
            next2 = cur.next.next
            cur.next = next2
            next1.next = next2.next
            next2.next = next1
            cur = cur.next.next
        return dummy_node.next

2.leetcode题目 19 删除链表的倒数第n个节点

题目描述:给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

我的思路:先得到链表长度N,再遍历到N-n个节点处执行删除操作。

解决:

# 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_node = ListNode(val = 0,next = head)
        cur = dummy_node
        N = 0
        while cur.next:
            N += 1
            cur = cur.next
        N_n = N - n
        c = 0
        temp = dummy_node
        for i in range(1,N_n + 1):
            temp = temp.next
        temp.next = temp.next.next
        return dummy_node.next

        

其他思路:①栈②双指针:

①思路:利用栈的先进后出原则

解决:

# 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_node = ListNode(val = 0,next = head)
        cur = dummy_node
        stack = list()
        while cur:
            stack.append(cur)
            cur = cur.next
        for i in range(n):
            stack.pop()
        prev = stack[-1]
        prev.next = prev.next.next
        return dummy_node.next

        

②双指针:

思路:让快指针先走n步,整体遍历的时候,快指针遍历完时,慢指针则刚好在倒数第n个的前一个。

# 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_node = ListNode(val = 0,next = head)
        second = dummy_node
        first = head
        for i in range(n):
            first = first.next
        while first:
            first = first.next
            second = second.next
        second.next = second.next.next
        return dummy_node.next

        

3.leetcode题目142 环形链表II:

.题目链接:142. 环形链表 II - 力扣(LeetCode)

解决:

①集合法(这是我直接想到的方法):

# 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]:
        cur = head
        res = set()
        while cur:
            if cur in res:
                return cur
            res.add(cur)
            cur = cur.next
        return None

②快慢指针法(这方法是真的想不到):

# 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]:
        fast = head
        slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast== slow:
                slow = head
                while fast!=slow:
                   fast = fast.next
                   slow = slow.next
                return slow
        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值