problemscpp
A collection of my answers to algorithm problems in c++.
Public 成员函数 | Private 属性 | 所有成员列表
leetcode::design_linked_list::MyLinkedList类 参考

#include <leetcode.h>

Public 成员函数

 MyLinkedList ()
 Initializes the MyLinkedList object. 更多...
 
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 addAtIndex (int index, int val)
 Add a node of value val before the indexth 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 addAtTail (int val)
 Append a node of value val as the last element of the linked list. 更多...
 
void deleteAtIndex (int index)
 Delete the indexth node in the linked list, if the index is valid. 更多...
 
int get (int index) const
 Get the value of the indexth node in the linked list. If the index is invalid, return -1. 更多...
 

Private 属性

ListNodehead
 
ListNodetail
 

详细描述

在文件 leetcode.h2694 行定义.

构造及析构函数说明

◆ MyLinkedList()

leetcode::design_linked_list::MyLinkedList::MyLinkedList ( )

Initializes the MyLinkedList object.

在文件 leetcode.cpp7323 行定义.

7323 {
7324 head = new ListNode();
7325 tail = head;
7326 }

引用了 head , 以及 tail.

成员函数说明

◆ addAtHead()

void leetcode::design_linked_list::MyLinkedList::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.

在文件 leetcode.cpp7340 行定义.

7340 {
7341 auto *const node = new ListNode(val);
7342 node->next = head->next;
7343 head->next = node;
7344 if(tail == head) {
7345 tail = node;
7346 }
7347 }
ListNode * next
Definition: leetcode.h:44

引用了 head, leetcode::ListNode::next , 以及 tail.

被这些函数引用 leetcode::design_linked_list::TEST().

◆ addAtIndex()

void leetcode::design_linked_list::MyLinkedList::addAtIndex ( int  index,
int  val 
)

Add a node of value val before the indexth 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.

在文件 leetcode.cpp7355 行定义.

7355 {
7356 index += 1;
7357 if(index < 1) {
7358 return;
7359 }
7360 auto *const node = new ListNode(val);
7361 ListNode *current = head;
7362 for(int i = 0; i < index - 1 && current != nullptr; i++) {
7363 current = current->next;
7364 }
7365 if(current == nullptr) {
7366 delete node;
7367 return;
7368 }
7369 node->next = current->next;
7370 current->next = node;
7371 if(current == tail) {
7372 tail = node;
7373 }
7374 }

引用了 head, leetcode::ListNode::next , 以及 tail.

被这些函数引用 leetcode::design_linked_list::TEST().

◆ addAtTail()

void leetcode::design_linked_list::MyLinkedList::addAtTail ( int  val)

Append a node of value val as the last element of the linked list.

在文件 leetcode.cpp7349 行定义.

7349 {
7350 auto *const node = new ListNode(val);
7351 tail->next = node;
7352 tail = node;
7353 }

引用了 leetcode::ListNode::next , 以及 tail.

被这些函数引用 leetcode::design_linked_list::TEST().

◆ deleteAtIndex()

void leetcode::design_linked_list::MyLinkedList::deleteAtIndex ( int  index)

Delete the indexth node in the linked list, if the index is valid.

在文件 leetcode.cpp7376 行定义.

7376 {
7377 index += 1;
7378 if(index < 1) {
7379 return;
7380 }
7381 ListNode *current = head;
7382 for(int i = 0; i < index - 1 && current != nullptr; i++) {
7383 current = current->next;
7384 }
7385 if(current == nullptr) {
7386 return;
7387 }
7388 const auto *const p = current->next;
7389 if(p == tail) {
7390 tail = current;
7391 }
7392 if(p != nullptr) {
7393 current->next = current->next->next;
7394 delete p;
7395 }
7396 }

引用了 head, leetcode::ListNode::next , 以及 tail.

被这些函数引用 leetcode::design_linked_list::TEST().

◆ get()

int leetcode::design_linked_list::MyLinkedList::get ( int  index) const

Get the value of the indexth node in the linked list. If the index is invalid, return -1.

在文件 leetcode.cpp7328 行定义.

7328 {
7329 index += 1;
7330 if(index < 1) {
7331 return -1;
7332 }
7333 const ListNode *current = head;
7334 for(int i = 0; i < index && current != nullptr; i++) {
7335 current = current->next;
7336 }
7337 return current == nullptr ? -1 : current->val;
7338 }

引用了 head, leetcode::ListNode::next , 以及 leetcode::ListNode::val.

被这些函数引用 leetcode::design_linked_list::TEST().

类成员变量说明

◆ head

ListNode* leetcode::design_linked_list::MyLinkedList::head
private

在文件 leetcode.h2695 行定义.

被这些函数引用 MyLinkedList(), addAtHead(), addAtIndex(), deleteAtIndex() , 以及 get().

◆ tail

ListNode* leetcode::design_linked_list::MyLinkedList::tail
private

在文件 leetcode.h2696 行定义.

被这些函数引用 MyLinkedList(), addAtHead(), addAtIndex(), addAtTail() , 以及 deleteAtIndex().


该类的文档由以下文件生成: