代码随想录算法训练营第三天| 203. 移除链表元素、707. 设计链表、206. 反转链表

本文介绍了三种关于链表的操作:一是移除具有特定值的链表元素,二是设计一个链表类实现添加、获取、删除等方法,三是反转链表。这些操作都涉及到对链表节点的遍历和修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

203. 移除链表元素:


代码思路

设置好pre前置节点,和cur现在节点这些变量,就好搞。
但是要注意,空节点是None,而None是没有next和val这些方法的。

# 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]:
        if head:
            pre = None
            cur = head
            while cur != None:  
                if cur.val == val:
                    if pre != None:
                        pre.next = cur.next
                        cur = cur.next
                    elif head != None:
                        head = head.next
                        cur = head
                        continue
                    elif head == None:
                        return None
                else:
                    pre = cur    
                    cur = cur.next                  
            return head
        else:
            return None

707. 设计链表


代码思路

该题是先构建节点的类,再在链表的类中调用节点类。

class Node:
    def __init__(self):
        self.val = None
        self.next = None


class MyLinkedList:

    def __init__(self):
        self.head = None

    def get(self, index: int) -> int:
        cur = self.head
        for i in range(index + 1):
            if i == index:
                if cur == None:
                    return -1
                return cur.val
            elif cur.next == None:
                return -1
            else:
                cur = cur.next

    def addAtHead(self, val: int) -> None:
        add_node = Node()
        add_node.val = val
        add_node.next = self.head
        self.head = add_node

    def addAtTail(self, val: int) -> None:
        cur = self.head
        tail = Node()
        tail.val = val
        while cur:
            if cur.next == None:
                cur.next = tail
                return None
            else:
                cur = cur.next
        self.head = tail

    def addAtIndex(self, index: int, val: int) -> None:
        insert_node = Node()
        insert_node.val = val
        cur = self.head
        if index == 0:
            insert_node.next = self.head
            self.head = insert_node
            return None
        n = 1
        if self.head:
            while n < index:
                if cur == None:
                    break
                else:

                    cur = cur.next
                n += 1
            if cur != None:
                insert_node.next = cur.next
                cur.next = insert_node

        elif index == 0:
            self.head = insert_node


    def deleteAtIndex(self, index: int) -> None:
        pre = None
        cur = self.head
        if cur:
            if index == 0:
                self.head = self.head.next
                return None
            n = 0
            while n < index:
                if cur.next == None:
                    return None
    
                else:
                    pre = cur
                    cur = cur.next
                n += 1
            pre.next = cur.next



# 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. 反转链表


代码思路

同样是设置好pre,和cur这些变量,就好搞。

# 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]:

        pre = None
        cur = head
        if cur:
            next = cur.next
            while next != None:
                next = cur.next
                if next == None:
                    cur.next = pre
                    break
                cur.next = pre
                pre = cur
                cur = next
            
        return cur
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值