datawhale-leetcode打卡:第026~037题

反转链表(leetcode 206)

这个题目我就比较流氓了,干脆新建链表翻转过来算了。但是完蛋,超出内存限制,那我就只能两两换了。这里比较大的技巧就是可以用一个空节点进行置换。

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

反转链表II(leetcode 092)

这个还是一样的思想,用空节点的方法解决这个问题。

class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        p0 = dummy = ListNode(next=head)
        for _ in range(left - 1):
            p0 = p0.next

        pre = None
        cur = p0.next
        for _ in range(right - left + 1):
            nxt = cur.next
            cur.next = pre  # 每次循环只修改一个 next,方便大家理解
            pre = cur
            cur = nxt

        # 见视频
        p0.next.next = cur
        p0.next = pre
        return dummy.next

K个一组反转链表(leetcode 025)

这个题号我怎么感觉这么乱呢,难度递增题号递减是吧

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        n = 0
        cur = head
        while cur:
            n += 1  # 统计节点个数
            cur = cur.next

        p0 = dummy = ListNode(next=head)
        pre = None
        cur = head
        while n >= k:
            n -= k
            for _ in range(k):  # 同 92 题
                nxt = cur.next
                cur.next = pre  # 每次循环只修改一个 next,方便大家理解
                pre = cur
                cur = nxt

            # 见视频
            nxt = p0.next
            nxt.next = cur
            p0.next = pre
            p0 = nxt
        return dummy.next

回文链表(leetcode 234)

这个题我终于可以使用流氓方法了哈哈哈哈

class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        s=[]
        while head.next:
            s.append(head.val)
            head=head.next
        s.append(head.val)
        return s==s[::-1]

合并两个有序链表(leetcode 021)

这个题本质是一个归并排序,遵循数据结构里面的,先排,然后谁剩下就把谁拼最后。

class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        p=ListNode()
        newhead=ListNode(next=p)
        if list1==None and list2==None:
            return None
        if list1==None:
            return list2
        if list2==None:
            return list1
        while list1 and list2:
            if list1.val>list2.val:
                p.next=list2
                list2=list2.next
            else:
                p.next=list1
                list1=list1.next
            p=p.next
        if list1:
            p.next=list1
        else:
            p.next=list2
        return newhead.next.next

排序链表(leetcode 148)

这个题比较简单,跟上面的方法一样

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        s=[]
        p=ListNode(next=head)
        q=ListNode(next=p)
        while head:
            s.append(head.val)
            head=head.next
        s.sort()
        p=p.next
        for i in s:
            p.val=i
            p=p.next
        return q.next.next

合并K个有序链表(leetcode 023)

这个题我们参考21题的代码进行融合就可以了。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    # 21. 合并两个有序链表
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        cur = dummy = ListNode()  # 用哨兵节点简化代码逻辑
        while list1 and list2:
            if list1.val < list2.val:
                cur.next = list1  # 把 list1 加到新链表中
                list1 = list1.next
            else:  # 注:相等的情况加哪个节点都是可以的
                cur.next = list2  # 把 list2 加到新链表中
                list2 = list2.next
            cur = cur.next
        cur.next = list1 if list1 else list2  # 拼接剩余链表
        return dummy.next

    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        m = len(lists)
        if m == 0: return None  # 注意输入的 lists 可能是空的
        if m == 1: return lists[0]  # 无需合并,直接返回
        left = self.mergeKLists(lists[:m // 2])  # 合并左半部分
        right = self.mergeKLists(lists[m // 2:])  # 合并右半部分
        return self.mergeTwoLists(left, right)  # 最后把左半和右半合并


环形链表(leetcode 141)

这个题目昨天面试的时候被问到过,马上就想到了快慢指针法。昨天刚手搓过一回

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

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        p=ListNode(next=head)
        q=ListNode(next=head)
        while p and q:
            p=p.next
            q=q.next.next
            if p==q:
                return True
        return False

环形链表II(leetcode 142)

这个解法是昨天面试搓的另一个方法。

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

相交链表(leetcode 160)

这道题是以往的一道考研原题,以前复习考研的时候做过。这道题应该是408在2011年的一道题目改编,当时是以字母的形式考察相交链表。本质上就是先移动长表,然后一起走。

# 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) -> Optional[ListNode]:
        p1=ListNode(next=headA)
        q1=ListNode(next=headB)
        p=ListNode(next=headA)
        q=ListNode(next=headB)
        len1,len2=0,0
        while p1:
            len1+=1
            p1=p1.next
        while q1:
            len2+=1
            q1=q1.next
        if len1>len2:
            for i in range(len1-len2):
                p=p.next
        if len2>len1:
            for i in range(len2-len1):
                q=q.next
        for i in range(min(len1,len2)-1):
            if p.next==q.next:
                return p.next
            p=p.next
            q=q.next
        return None 

删除链表的倒数第N个节点(leetcode 019)

疯狂try&except去调试最后跑过了几个案例。

# 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]:
        p=ListNode(next=head)
        q=ListNode(next=head)
        h=ListNode(next=head)
        count=0
        for i in range(n):
            try:
                q=q.next
                count+=1
            except:
                return None
        try:
            while q.next:
                p=p.next
                q=q.next
                count+=1
        except:
            pass
        print(count)
        if count==n:
            return None
        p.next=p.next.next
        return h.next
        

重排链表(leetcode 143)

解法仍然是快慢指针法。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reorderList(self, head: Optional[ListNode]) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        fast = slow = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        cur = slow.next
        slow.next = None
        pre = None
        while cur:
            t = cur.next
            cur.next = pre
            pre, cur = cur, t
        cur = head
        while pre:
            t = pre.next
            pre.next = cur.next
            cur.next = pre
            cur, pre = pre.next, t
posted @   白纸画卷水墨如冰  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示