算法提高之LeetCode刷题数据结构和算法分析

LeetCode 707. 设计链表

2019-01-06  本文已影响2人  SmallRookie

题目描述

题解

struct MyListNode {
    int val;
    MyListNode* next;
};
class MyLinkedList {
public:
    MyListNode* head;
    /** Initialize your data structure here. */
    MyLinkedList() {
        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;
        if(head != NULL) {
            MyListNode* tmp = head;
            while(index--) {
                tmp = tmp->next;
                if(tmp == NULL) return -1;
            }
            return tmp->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) {
        MyListNode* newHead = new MyListNode;
        newHead->val = val;
        newHead->next = head;
        head = newHead;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        MyListNode* newTail = new MyListNode;
        newTail -> val = val;
        newTail -> next = NULL;
        
        if(head != NULL) {
            MyListNode* tmp = head;
            while(tmp->next != NULL) {
                tmp = tmp->next;
            }
            tmp->next = newTail;
        } else {
            head = newTail;
        }
    }
    
    /** 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) return;
        if(index == 0) {
            addAtHead(val);
            return;
        }
        MyListNode* newNode = new MyListNode;
        newNode->val = val;
        newNode->next = NULL;
        if(head != NULL) {
            MyListNode* tmp = head;
            while(--index) {
                tmp = tmp->next;
                if(tmp == NULL) return;
            }
            newNode->next = tmp->next;
            tmp->next = newNode;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index < 0) return;
        
        if(index == 0) {
            delete(head);
            head = NULL;
            return;
        }
        
        MyListNode* pre = head;
        MyListNode* nodeToDelete = NULL;
        while(--index) {
            pre = pre->next;
            if(pre == NULL) return;
        }
        nodeToDelete = pre->next;
        if(nodeToDelete == NULL) return;
        
        pre->next = nodeToDelete->next;
        delete(nodeToDelete);
    }
};

/**
 * 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);
 */
上一篇 下一篇

猜你喜欢

热点阅读