707 - Design Linked List : 이중 연결리스트 ver.
2021. 6. 19. 02:54ㆍ알고리즘 관련/Linked List
https://leetcode.com/problems/design-linked-list/
작성 코드:
struct Node {
int val;
Node *next, *prev;
Node(int x) : val(x), next(nullptr), prev(nullptr) {}
};
class MyLinkedList
{
private:
Node *head;
Node *tail;
int length;
public:
/** Initialize your data structure here. */
MyLinkedList()
{
head = nullptr;
tail = nullptr;
length = 0;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index)
{
Node *cur = head;
if (length <= index)
return -1;
for (int i = 0; index > i; i++)
cur = cur->next;
return cur->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 addAtHead(int val)
{
Node *newNode = new Node(val);
newNode->next = head;
if (head != nullptr)
head->prev = newNode;
head = newNode;
if (length == 0) {
tail = head;
}
length++;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val)
{
Node *newNode = new Node(val);
if (tail != nullptr)
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
length++;
if (length == 1)
head = tail;
}
/** Add a node of value val before the index-th node in the linked list.
* If index equals to the length of linked list, the node will be appended to the end of linked list.
* If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val)
{
if (index > length || index < 0) return;
if (index == length) {
addAtTail(val);
return;
}
if (index == 0) {
addAtHead(val);
return;
}
Node *newNode = new Node(val);
Node *cur = head;
for (int i = 0; index > i; i++)
cur = cur->next;
newNode->prev = cur->prev;
newNode->next = cur;
cur->prev->next = newNode;
cur->prev = newNode;
length++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index)
{
if (length == 0 || index >= length)
return;
else if (index == 0) {
head = head->next;
length--;
return;
}
Node *cur = head;
for (int i = 0; index > i; i++)
cur = cur->next;
if (cur->next != nullptr )
cur->next->prev = cur->prev;
cur->prev->next = cur->next;
if (tail == cur)
tail = cur->prev;
delete cur;
length--;
}
};
좀 더 효율적인 코드
class MyNode {
public:
int val;
MyNode* next;
MyNode* prev;
MyNode(int val) {
this->val = val;
}
};
class MyLinkedList {
private:
MyNode* head;
MyNode* tail;
int size = 0;
// link a -> b -> a_next
void link(MyNode* a, MyNode* b) {
b->prev = a;
b->next = a->next;
a->next->prev = b;
a->next = b;
}
public:
/** Initialize your data structure here. */
MyLinkedList() {
head = new MyNode(1e9);
tail = new MyNode(-1e9);
head->next = tail;
tail->prev = head;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if(size < index+1) return -1;
MyNode* current = head;
for(int i = 0; i <= index; i++) current = current->next;
return current->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 addAtHead(int val) {
MyNode* newNode = new MyNode(val);
link(head, newNode);
size++;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
MyNode* newNode = new MyNode(val);
link(tail->prev, newNode);
size++;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
if(size < index) return;
MyNode* current = head;
for(int i = 0; i < index; i++) current = current->next;
MyNode* newNode = new MyNode(val);
link(current, newNode);
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if(size < index+1) return;
MyNode* current = head;
for(int i = 0; i <= index; i++) current = current->next;
current->next->prev = current->prev;
current->prev->next = current->next;
size--;
}
};
연결리스트를 연결시키는 link함수를 정의함으로써, 중복되는 코드를 줄여 코드가 비교적 간결하다.
'알고리즘 관련 > Linked List' 카테고리의 다른 글
328. Odd Even Linked List (0) | 2021.06.20 |
---|---|
206. Reverse Linked List (0) | 2021.06.19 |
19. Remove Nth Node From End of List (0) | 2021.06.18 |
160. Intersection of Two Linked Lists (0) | 2021.06.18 |
142. Linked List Cycle II (0) | 2021.06.17 |