代码随想录算法训练营第三天 | LeetCode203移除链表元素、LeetCode707设计链表、LeetCode206反转链表

代码随想录算法训练营第三天 | LeetCode203移除链表元素、LeetCode707设计链表、LeetCode206反转链表

时长:大约3小时

203. Remove Linked List Elements

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head .

Example 1:

Example 2:

Example 3:

Constraints:

The number of nodes in the list is in the range [0, 10<sup>4</sup>].

1 <= Node.val <= 50

0 <= val <= 50

解题思路:

主要注意处理头部删除和中间删除,可以添加dummyHead来统一这两者

需要注意的要点:

添加了dummyHead

要返回dummyHead->next

从dummyHead开始循环,处理cur->next

707. Design Linked List

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed .

Implement the MyLinkedList class:

MyLinkedList() Initializes the MyLinkedList object.

int get(int index) Get the value of the index<sup>th</sup> node in the linked list. If the index is invalid, return -1.

void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.

void addAtTail(int val) Append a node of value val as the last element of the linked list.

void addAtIndex(int index, int val) Add a node of value val before the index<sup>th</sup> node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted .

void deleteAtIndex(int index) Delete the index<sup>th</sup> node in the linked list, if the index is valid.

Example 1:

Constraints:

0 <= index, val <= 1000

Please do not use the built-in LinkedList library.

At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.

解题思路:

按步骤实现每一部分的功能即可

可以采用dummyHead来统一处理元素添加

中间添加

头部添加

删除是对cur->next处理,从dummyHead开始循环

添加也是对cur->next处理,从dummyHead开始循环

可以记录元素数目对index进行判断

需要注意的要点:

addIndex的index可以等于当前的链表,意味着在尾部添加

206. Reverse Linked List

Given the head of a singly linked list, reverse the list, and return the reversed list .

Example 1:

Example 2:

Example 3:

Constraints:

The number of nodes in the list is the range [0, 5000].

-5000 <= Node.val <= 5000

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

解题思路:

需要记录三个变量:

prev

cur

next

把cur->next转为prev,然后进到next,循环或迭代处理。

需要注意的要点:

需要记录next,不然没办法进入下一个节点处理

返回的时候由于cur为NULL,返回prev即可

声明:

文章中LeetCode的题目来自LeetCode官网:https://leetcode.cn/problems

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值