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

目录

前置知识

203.移除链表元素 

707.设计链表 

206.反转链表 


前置知识
建议:了解一下链接基础,以及链表和数组的区别 

文章链接:代码随想录


203.移除链表元素 

建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。

题目链接/文章讲解/视频讲解::代码随想录

思路:

        只要对链表有一定了解的话应该嘎嘎写出来(可惜博主全忘光了。。。)简单讲一下,用ListNode类,存放每一个节点的val和next节点的位置。创建一个虚拟(virtual)头结点virhead(应该是为了避免头节点的元素就是待移除元素而需要额外去考虑),有了virhead,最后只要返回virhead.next即可。其余细节直接看上面链接,博主已经懒得打了。

代码:

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

from typing import Optional
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        virhead = ListNode()
        virhead.next = head
        cur = virhead
        while cur.next is not None:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur=cur.next
        return virhead.next

aa=Solution()
head=eval(input())
val=int(input())
aa.removeElements(head,val)

707.设计链表 

建议: 这是一道考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点

题目链接/文章讲解/视频讲解:代码随想录

        这道题我觉得很有价值,把链表的基本操作差不多都覆盖到了,如果自己能把整道题完全写出来,链表的基础知识就算还有可以吧。(大家真的要记笔记,这些大二上的数据结构与算法都学过,现在全忘了,绷不住了)

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


class MyLinkedList:

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

    def get(self, index: int) -> int:
        if index < 0 or index >= self.size:  # 这里是>= 等于也是不可取的,自己思考一下
            return -1

        current = self.dummy_head.next  # 注意这里有.next 可以自己推导一下
        for _ in range(index):
            current = current.next
        return current.val

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

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

        current = self.dummy_head  # 只有上面那个有next  其他的current没有
        while current.next:
            current = current.next
        current.next = ListNode(val)
        self.size += 1

    def addAtIndex(self, index: int, val: int) -> None:
        if index < 0 or index > self.size:
            return

        current = self.dummy_head
        for _ in range(index):
            current = current.next
        current.next = ListNode(val, current.next)
        self.size += 1

    def deleteAtIndex(self, index: int) -> None:
        if index < 0 or index >= self.size:
            return
        current = self.dummy_head
        for _ in range(index):
            current = current.next
        current.next = current.next.next
        self.size -= 1


obj = MyLinkedList()
param_1 = obj.get(2)
obj.addAtHead(4)
obj.addAtTail(4)
obj.addAtIndex(6, 8)
obj.deleteAtIndex(9)
206.反转链表 

题目链接/文章讲解/视频讲解:代码随想录

思路:

        如果,你也是小白,那么,我觉得,这个思路,泰裤辣(bushi)

        我第一反应是把所有元素存进一个列表里,然后用reverse()函数反转后一个个塞回去(笑晕了) ,后面一想就知道不对,数据量大一点,这要存到什么时候。

        Carl的答案就是常规答案,翻转指针,维护三个变量,precurtemp,注意要先把cur.next赋值给temp,不然后面的结点全都索引不到了。最后在写pre=cur, cur=temp

        还有一个要注意的点,while的判定条件不能写while current.next:  这样写最后一个结点不会被翻转过来!!!最后返回pre即可。

代码:


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

from typing import Optional
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        pre = None
        current=head
        while current:   # 不能是current.next  得是current  不然最后一个点无法反转
            temp=current.next
            current.next=pre
            pre=current
            current=temp
        return pre

麻了,这两天又是王者打比赛,又是算法训练营刚起步,又是补打卡的一天,趁星期天没有任务把星期六的补补吧。

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道了。以下是对您的问题的回答: a. 动态分配内存的方法有很多,可以使用C语言的malloc函数,也可以使用C++中的new操作符,具体方法如下: C语言的malloc函数: ```c struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); ``` C++中的new操作符: ```c++ ListNode* node = new ListNode(); ``` b. 初始化单链表需要对链表节点的成员变量进行赋值,以下是一个示例: ```c++ struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* head = NULL; ``` c. 头插法创建单链表的示例代码如下: ```c++ ListNode* head = new ListNode(); head->next = NULL; for (int i = 0; i < n; i++) { int x; cin >> x; ListNode* node = new ListNode(x); node->next = head->next; head->next = node; } ``` 尾插法创建单链表的示例代码如下: ```c++ ListNode* head = new ListNode(); head->next = NULL; ListNode* tail = head; for (int i = 0; i < n; i++) { int x; cin >> x; ListNode* node = new ListNode(x); node->next = NULL; tail->next = node; tail = node; } ``` d. 取链表某一位的值可以通过遍历链表来实现,以下是一个示例: ```c++ ListNode* p = head->next; int pos = 3; for (int i = 1; i < pos && p != NULL; i++) { p = p->next; } if (p == NULL) { cout << "链表长度不足" << endl; } else { cout << p->val << endl; } ``` e. 在链表的某一位进行插入操作可以通过遍历链表找到插入位置,然后插入一个新节点来实现,以下是一个示例: ```c++ ListNode* p = head; int pos = 3; for (int i = 1; i < pos && p != NULL; i++) { p = p->next; } if (p == NULL) { cout << "链表长度不足" << endl; } else { int x = 100; ListNode* node = new ListNode(x); node->next = p->next; p->next = node; } ``` f. 遍历链表可以通过循环遍历链表节点来实现,以下是一个示例: ```c++ ListNode* p = head->next; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值