当前位置:网站首页>2.2 linked list - Design linked list (leetcode 707)
2.2 linked list - Design linked list (leetcode 707)
2022-06-12 08:04:00 【Amoni_】
class MyLinkedList {
public:
// Define linked list node structure
struct ListNode {
int val;
ListNode* next;
ListNode(): val(0), next(nullptr) {
}
ListNode(int x): val(x), next(nullptr) {
}
ListNode(int x, ListNode* next): val(x), next(next) {
}
};
// Constructors
MyLinkedList() {
_dummyHead = new ListNode(0);// The head node defined here It's a virtual head node , Instead of the real chain header node
_size = 0;
}
// Get to page No index Node values , If index It's illegal. It's returned directly -1, Be careful index It's from 0 At the beginning , The first 0 The first node is the head node
int get(int index) {
if(index > (_size-1) || index < 0){
// illegal value (_size To calculate the dummyHead, Therefore need -1)
return -1;
}
ListNode* cur = _dummyHead->next; // Initialize as the head node
while(index--){
cur = cur->next;
}
return cur->val;
}
// Insert a node at the front of the linked list , After insertion , The newly inserted node is the new head node of the linked list
void addAtHead(int val) {
ListNode* newNode = new ListNode(val);
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;
}
// Add a node to the last side of the list
void addAtTail(int val) {
ListNode* newNode = new ListNode(val);
ListNode* cur = _dummyHead; // Notice that this is _dummyHead
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = newNode;
_size++;
}
// In the list, the index The value added before nodes is val The node of .
// 1. If index It's equal to the length of the list , Then the node will be attached to the end of the list .
// 2. If index Greater than the length of the list , The node will not be inserted .
// 3. If index Less than 0, Then insert the node in the head .
void addAtIndex(int index, int val) {
if(index > _size){
return ;
}
ListNode* newNode = new ListNode(val);
ListNode* cur = _dummyHead; // Notice that this is _dummyHead
while(index--){
cur = cur->next;
}
newNode->next = cur->next; // Pay attention to the connection sequence
cur->next = newNode;
_size++;
}
// If the index index It works , Delete the index Nodes
void deleteAtIndex(int index) {
if(index >= _size || index < 0){
return ;
}
ListNode* cur = _dummyHead; // Notice that this is _dummyHead
while(index--){
cur = cur->next;
}
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size--;
}
private:
int _size ;
ListNode* _dummyHead;
};
/** * Your MyLinkedList object will be instantiated and called as such: * MyLinkedList* obj = new MyLinkedList(); * int param_1 = obj->get(index); * obj->addAtHead(val); * obj->addAtTail(val); * obj->addAtIndex(index,val); * obj->deleteAtIndex(index); */
边栏推荐
- ECMAScript6面试题
- FPGA based communication system receiver [packet detection] development document
- 謀新局、促發展,桂林綠色數字經濟的頭雁效應
- Transformation from AC5 to AC6 (1) - remedy and preparation
- Servlet advanced
- 『Three.js』辅助坐标轴
- Leetcode notes: Weekly contest 280
- Support vector machine (SVM)
- 20220526 损失函数
- The R language uses the sample The split function divides the machine learning data set into training set and test set
猜你喜欢

Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy

20220526 yolov1-v5

Database connection pool and dbutils tool

Vscode 1.68 changes and concerns (sorting and importing statements / experimental new command center, etc.)

Vision Transformer | Arxiv 2205 - TRT-ViT 面向 TensorRT 的 Vision Transformer

从AC5到AC6转型之路(1)——补救和准备

Pytorch profiler with tensorboard.

『Three.js』辅助坐标轴

Explanation and explanation on the situation that the volume GPU util (GPU utilization) is very low and the memory ueage (memory occupation) is very high during the training of pytoch

HDLC protocol
随机推荐
Work summary of the week from November 22 to 28, 2021
Final review of Discrete Mathematics (predicate logic, set, relation, function, graph, Euler graph and Hamiltonian graph)
R language dplyr package mutate_ At function and one_ The of function converts the data type of a specified data column (specified by a vector) in dataframe data to a factor type
解决上传SFTPorg.apache.commons.net.MalformedServerReplyException: Could not parse respon
qt. qpa. plugin: Could not load the Qt platform plugin “xcb“ in “***“
Vscode 1.68 changes and concerns (sorting and importing statements / experimental new command center, etc.)
移动端、安卓、IOS兼容性面试题
Symfony 2: multiple and dynamic database connections
20220524 深度学习技术点
Rnorm function of R language generates positive distribution data, calculates descriptive statistical summary information of vector data using sum function of epidisplay package, and visualizes ordere
Topic 1 Single_ Cell_ analysis(4)
Kalman filter encapsulation function
Improvement of hash function based on life game
2D visualization of oil storage, transportation and production, configuration application enables intelligent development of industry
Improvement of hash function based on life game (continued 2)
Leetcode notes: biweekly contest 71
802.11 protocol: wireless LAN protocol
Leetcode notes: Weekly contest 278
20220607. face recognition
N-order nonzero matrix AB, matrix ab=0, then the rank of a and B is less than n