代码随想录python笔记2 链表

在这里插入图片描述

203.移除链表元素

力扣题目链接(opens new window)

题意:删除链表中等于给定值 val 的所有节点。

示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]

示例 2:
输入:head = [], val = 1
输出:[]

示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
         # 在原有链表基础上修改
         while head != None and head.val == val:
             head = head.next
         cur = head
         while cur != None and cur.next != None:
             if cur.next.val == val:
                 cur.next = cur.next.next
             else:
                cur = cur.next
        return head
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        # 虚拟头节点
         dummy_head = ListNode(next=head)
         cur = dummy_head
         while  cur.next :
             if cur.next.val == val:
                 cur.next = cur.next.next
             else:
                 cur = cur.next
         return dummy_head.next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        # 递归
        if head is None:
            return head
        head.next = self.removeElements(head.next,val)
        return head.next if head.val == val else head 

707.设计链表

力扣题目链接(opens new window)

题意:

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

707示例

class Node:
    def __init__(self, val):
        # 单链表
        self.val = val
        self.next = None
class MyLinkedList:
    def __init__(self):
        self._head = Node(0) # 虚拟头节点,随便设置了个0的初始值
        self._count = 0 # 统计节点数量 
    def get(self, index: int) -> int:
        if 0 <= index < self._count:
            cur = self._head
            for i in range(index + 1):
                cur = cur.next
            return cur.val
        else:
            return -1
    def addAtHead(self, val: int) -> None:
        self.addAtIndex(0, val)

    def addAtTail(self, val: int) -> None:
        self.addAtIndex(self._count,val)

    def addAtIndex(self, index: int, val: int) -> None:
        if index > self._count:
            return
        index = max(0, index)
        self._count += 1
        pre = self._head
        for i in range(index): # 遍历index前面的链表
            pre = pre.next
        add_Node = Node(val)
        add_Node.next = pre.next
        pre.next = add_Node

    def deleteAtIndex(self, index: int) -> None:
        if 0 <= index < self._count:
            self._count -= 1
            pre = self._head
            for i in range(index): # 遍历index前面的链表
                pre = pre.next
            pre.next = pre.next.next

206.反转链表

力扣题目链接(opens new window)

题意:反转一个单链表。

示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL

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

力扣题目链接(opens new window)

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

24.两两交换链表中的节点-题意
# 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 = ListNode(0)
        dummy.next = head
        pre = dummy
        while pre.next and pre.next.next:
            cur, post = pre.next, pre.next.next
            pre.next = post
            cur.next = post.next
            post.next = cur
            pre = pre.next.next 
        return dummy.next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0)
        dummy.next = head
        pre = dummy
        while pre.next and pre.next.next:
            tmp1, tmp2 = pre.next, pre.next.next.next # 保存临时节点 (1),2,(3),4
            pre.next = pre.next.next  # 步骤一:pre -> 2
            pre.next.next = tmp1  # 步骤二:2 -> 1
            pre.next.next.next = tmp2  # 步骤三: 1 -> 3
            pre = pre.next.next # 迭代
        return dummy.next

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

力扣题目链接(opens new window)

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

进阶:你能尝试使用一趟扫描实现吗?

示例 1:

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

输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] 示例 2:

输入:head = [1], n = 1 输出:[] 示例 3:

输入:head = [1,2], n = 1 输出:[1]

# 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: ListNode, n: int) -> ListNode:
        dummy_head = ListNode()
        dummy_head.next = head
        slow = dummy_head
        fast = dummy_head
        # while n != 0:
        #     fast = fast.next
        #     n -= 1
        for i in range(n):
            fast = fast.next
        while fast.next != None:
            slow = slow.next
            fast = fast.next
        slow.next = slow.next.next
        return dummy_head.next

面试题 02.07. 链表相交

同:160.链表相交

力扣题目链接(opens new window)

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

图示两个链表在节点 c1 开始相交:

img

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

示例 1:

img
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        cur_A, cur_B = headA, headB
        while cur_A != cur_B:
            cur_A = cur_A.next if cur_A != None else headB
            cur_B = cur_B.next if cur_B != None else headA
        return cur_A

142.环形链表II

力扣题目链接(opens new window)

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

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

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

循环链表
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast =  fast.next.next
            if slow == fast:
                p, q = head, slow
                while p != q:
                    p, q = p.next, q.next
                return p
        return None

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值