707. Design Linked List
https://leetcode.com/problems/design-linked-list/
Language: C++
https://leetcode.com/explore/learn/card/linked-list/209/singly-linked-list/1298/
// Definition for singly-linked list.
struct SinglyListNode {
int val;
SinglyListNode *next;
SinglyListNode(int x) : val(x), next(NULL) {}
};
class MyLinkedList {
private:
SinglyListNode *head;
public:
/** Initialize your data structure here. */
MyLinkedList() {
head = NULL;
}
};
/** Helper function to return the index-th node in the linked list. */
SinglyListNode* getNode(int index) {
SinglyListNode *cur = head;
for (int i = 0; i < index && cur; ++i) {
cur = cur->next;
}
return cur;
}
/** Helper function to return the last node in the linked list. */
SinglyListNode* getTail() {
SinglyListNode *cur = head;
while (cur && cur->next) {
cur = cur->next;
}
return cur;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
SinglyListNode *cur = getNode(index);
return cur == NULL ? -1 : 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) {
SinglyListNode *cur = new SinglyListNode(val);
cur->next = head;
head = cur;
return;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
if (head == NULL) {
addAtHead(val);
return;
}
SinglyListNode *prev = getTail();
SinglyListNode *cur = new SinglyListNode(val);
prev->next = cur;
}
/** 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 == 0) {
addAtHead(val);
return;
}
SinglyListNode *prev = getNode(index - 1);
if (prev == NULL) {
return;
}
SinglyListNode *cur = new SinglyListNode(val);
SinglyListNode *next = prev->next;
cur->next = next;
prev->next = cur;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
SinglyListNode *cur = getNode(index);
if (cur == NULL) {
return;
}
SinglyListNode *prev = getNode(index - 1);
SinglyListNode *next = cur->next;
if (prev) {
prev->next = next;
} else {
// modify head when deleting the first node.
head = next;
}
}
다른 solution
class MyLinkedList {
public:
/** Initialize your data structure here. */
class Node {
public:
int val;
Node* next;
Node(int val) {
this->val = val;
this->next = NULL;
}
};
int length;
Node* head;
MyLinkedList() {
length = 0;
head = NULL;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if (index < 0)
return -1;
int i = 0;
Node* node = head;
while (i < index) {
node = node->next;
i++;
}
if (node)
return node->val;
else
return -1;
}
/** 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) {
addAtIndex(0, val);
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
addAtIndex(length, val);
}
/** 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 < 0 || index > length)
return ;
Node* node = head;
Node* newNode = new Node(val);
if (index == 0) {
if (!node)
head = newNode;
else {
newNode->next = head;
head = newNode;
}
} else if (index == length) {
if (!node)
head = newNode;
else {
while (node->next) {
node = node->next;
}
node->next = newNode;
}
} else {
int i = 1; // 0
while (i != index) { // i < index - 1
node = node->next;
i++;
}
newNode->next = node->next;
node->next = newNode;
}
length++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if (index < 0 || index >= length)
return ;
Node* node = head;
if (index == 0) {
head = head->next;
node->next = nullptr;
}
else {
int i = 1;
while (i != index) {
node = node->next;
i++;
}
// prev->next = node->next;
node->next = node->next->next;
}
length--;
}
};
/**
* 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);
*/
문제점
복잡하게 짜고 싶지 않았었다; 계속 새벽 5시에 자고 계절학기까지 들으니까,, (열심히 듣지는 않지만 ㅎㅎ 내 피 같은 돈..)
집중 안되서 대충 마무리했었는데 좀 더 쉽고 깔끔하게 다시 도전해보고 싶은 문제.... 문제 수준도 아니다만ㅠㅠㅠㅠㅠ
728x90
반응형
'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글
Linked List Cycle II (0) | 2021.07.01 |
---|---|
Linked List Cycle (0) | 2021.06.29 |
Serialize and Deserialize Binary Tree (0) | 2021.06.27 |
Lowest Common Ancestor of a Binary Tree (0) | 2021.06.26 |
Populating Next Right Pointers in Each Node II (0) | 2021.06.25 |