LeetCode刷题(python3)|day03 第二章 链表part01

本文介绍了如何在Python中实现链表的操作,包括删除指定值的节点、设计链表类并处理边界条件,以及反转链表的方法。
摘要由CSDN通过智能技术生成

203.移除链表元素

# 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]:
        virtualHead = ListNode(-1, head)
        pre = virtualHead
        cur = virtualHead.next
        while cur != None:
            if cur.val == val:
                cur = cur.next
                pre.next = cur
            else:
                pre = cur
                cur = cur.next
        return virtualHead.next
                

707.设计链表
卡bug卡了很久,对于边界条件不够清晰

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class MyLinkedList:

    def __init__(self):
        self.dummyHead = ListNode()
        self.size = 0

    def get(self, index: int) -> int:
        if index < 0 or index >= self.size:
            return -1
        cur = self.dummyHead.next
        n = index
        while n > 0:
            cur = cur.next
            n -= 1
        return cur.val


    def addAtHead(self, val: int) -> None:
        newNode = ListNode(val)
        newNode.next = self.dummyHead.next
        self.dummyHead.next = newNode
        self.size += 1

    def addAtTail(self, val: int) -> None:
        newNode = ListNode(val, None)
        cur = self.dummyHead
        while cur.next != None:
            cur = cur.next
        cur.next = newNode
        self.size += 1

    def addAtIndex(self, index: int, val: int) -> None:
        if index < 0 or index > self.size:
            return
        newNode = ListNode(val, None)
        n = index
        pre = self.dummyHead
        while n > 0:
            pre = pre.next
            n -= 1
        newNode.next = pre.next
        pre.next = newNode
        self.size += 1
        
    def deleteAtIndex(self, index: int) -> None:
        if index < 0 or index >= self.size:
            return
        n = index
        pre = self.dummyHead
        while n > 0:
            pre = pre.next
            n -= 1
        if pre.next != None:
            pre.next = pre.next.next
            self.size -= 1




# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)

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]:
        vitualHead1 = ListNode(0, head)
        vitualHead2 = ListNode()

        first = True
        while vitualHead1.next != None:
            cur = vitualHead1.next
            vitualHead1.next = vitualHead1.next.next
            cur.next = vitualHead2.next
            vitualHead2.next = cur
        return vitualHead2.next
            


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值