dengxinyi 5 år sedan
förälder
incheckning
f50d978ef2
1 ändrade filer med 151 tillägg och 0 borttagningar
  1. 151 0
      leetcode/medium/707.cc

+ 151 - 0
leetcode/medium/707.cc

@@ -0,0 +1,151 @@
+/* BEGIN */
+
+struct MyListNode {
+    int val;
+    MyListNode *next;
+    MyListNode(int val, MyListNode *next = nullptr) : val(val), next(next) {
+    }
+};
+
+class MyLinkedList {
+private:
+    int size_;
+    MyListNode *head_;
+    MyListNode *tail_;
+
+public:
+    /** Initialize your data structure here. */
+    MyLinkedList() : size_(0), head_(nullptr), tail_(nullptr) {
+    }
+    
+    /** 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 || size_ <= index) {
+            return -1;
+        }
+        MyListNode *curr = head_;
+        while (index > 0) {
+            curr = curr->next;
+            index--;
+        }
+        return curr->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) {
+        MyListNode *tmp = new MyListNode(val, head_);
+        head_ = tmp;
+        if (size_ == 0) {
+            tail_ = head_;
+        }
+        size_++;
+    }
+
+    /** Append a node of value val to the last element of the linked list. */
+    void addAtTail(int val) {
+        if (size_ == 0) {
+            addAtHead(val);
+            return;
+        }
+        tail_->next = new MyListNode(val);
+        tail_ = tail_->next;
+        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 (index < 0 || size_ < index) {
+            return;
+        }
+        if (index == 0) {
+            addAtHead(val);
+            return;
+        }
+        if (index == size_) {
+            addAtTail(val);
+            return;
+        }
+        MyListNode *prev = head_;
+        index--;
+        while (index > 0) {
+            prev = prev->next;
+            index--;
+        }
+        MyListNode *tmp = new MyListNode(val, prev->next);
+        prev->next = tmp;
+        size_++;
+    }
+    
+    /** Delete the index-th node in the linked list, if the index is valid. */
+    void deleteAtIndex(int index) {
+        if (index < 0 || size_ <= index) {
+            return;
+        }
+        if (index == 0) {
+            MyListNode *tmp = head_;
+            head_ = head_->next;
+            delete tmp;
+            if (size_ == 1) {
+                tail_ = nullptr;
+            }
+        } else {
+            MyListNode *prev = head_;
+            index--;
+            while (index > 0) {
+                prev = prev->next;
+                index--;
+            }
+            MyListNode *tmp = prev->next;
+            prev->next = prev->next->next;
+            delete tmp;
+            if (prev->next == nullptr) {
+                tail_ = prev;
+            }
+        }
+        size_--;
+    }
+};
+
+
+/**
+ * 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);
+ */
+
+/* END */
+
+#include <cstdio>
+
+void my_print_list(MyLinkedList *list) {
+    int index = 0, val = -1;
+    while ((val = list->get(index)) != -1) {
+        printf("%d ", val);
+        index++;
+    }
+    printf("\n");
+}
+
+int main() {
+    MyLinkedList *obj = new MyLinkedList();
+    int param_1 = obj->get(0);
+    my_print_list(obj);
+
+    obj->addAtHead(1);
+    my_print_list(obj);
+
+    obj->addAtTail(100);
+    my_print_list(obj);
+
+    obj->addAtIndex(1, 5);
+    my_print_list(obj);
+
+    obj->deleteAtIndex(0);
+    my_print_list(obj);
+
+    return 0;
+}