Leetcode-707. 設計鏈表(C++)

設計鏈表的實現。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節點應該具有兩個屬性:val 和 next。val 是當前節點的值,next 是指向下一個節點的指針/引用。如果要使用雙向鏈表,則還需要一個屬性 prev 以指示鏈表中的上一個節點。假設鏈表中的所有節點都是 0-index 的。
在鏈表類中實現這些功能:

    get(index):獲取鏈表中第 index 個節點的值。如果索引無效,則返回-1。
    addAtHead(val):在鏈表的第一個元素之前添加一個值爲 val 的節點。插入後,新節點將成爲鏈表的第一個節點。
    addAtTail(val):將值爲 val 的節點追加到鏈表的最後一個元素。
    addAtIndex(index,val):在鏈表中的第 index 個節點之前添加值爲 val  的節點。如果 index 等於鏈表的長度,則該節點將附加到鏈表的末尾。如果 index 大於鏈表長度,則不會插入節點。
    deleteAtIndex(index):如果索引 index 有效,則刪除鏈表中的第 index 個節點。

示例:

    MyLinkedList linkedList = new MyLinkedList();
    linkedList.addAtHead(1);
    linkedList.addAtTail(3);
    linkedList.addAtIndex(1,2);   //鏈表變爲1-> 2-> 3
    linkedList.get(1);            //返回2
    linkedList.deleteAtIndex(1);  //現在鏈表是1-> 3
    linkedList.get(1);            //返回3

提示:

    所有值都在 [1, 1000] 之內。
    操作次數將在  [1, 1000] 之內。
    請不要使用內置的 LinkedList 庫。

解答

class MyLinkedList {
	
public:
	class ListNode {
	public:
		ListNode *next;
		int val;
		ListNode() {
		}

	    ListNode(int val) {
			this->val = val;
			this->next = nullptr;
		}
	};
	/** Initialize your data structure here. */
	 MyLinkedList(){
		 ListNode *head= new ListNode;
		 this->head = head;
		 this->head->next = nullptr;
	}

	/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
	int get(int index) {
	   //默認第一個元素索引是0
		if (index > this->size()-1 || index < 0) return -1;
		ListNode *cur = this->head;
		for (int i = 0; i <=index; 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) {

		ListNode *cur = new ListNode(val);
		//空鏈表則直接加到head
		if (nullptr == this->head->next)
		{
			this->head->next = cur;
			return;
		}
		//否則把head指向的地址轉移給cur的指針
		cur->next = this->head->next;
		//然後head指向新的結點
		this->head->next = cur;

	}

	/** Append a node of value val to the last element of the linked list. */
	void addAtTail(int val) {

		ListNode *newNode = new ListNode(val);
		ListNode *cur = this->head;
		if (nullptr == cur->next)
		{
			this->head->next = cur;
			return;
		}
		while (cur->next != nullptr) cur = cur->next;
		cur->next = newNode;
	}

	/** 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 > this->size() || -index > this->size()+1) return;
		//轉成正索引
		if (index < 0)
		{
			index = this->size()-1 - index;
		}
		if (index == 0) {
			this->addAtHead(val);
			return;
		}
		if (index == this->size()) {
			this->addAtTail(val);
			return;
		}
		ListNode *newNode = new ListNode(val);
		ListNode *pre = this->head;
		for (int i = 0; i < index; i++) pre = pre->next;
		newNode->next = pre->next;
		pre->next = newNode;
	}

	/** Delete the index-th node in the linked list, if the index is valid. */
	void deleteAtIndex(int index) {
		if (index < 0 || index > this->size() || NULL ==this->head->next) return;
		if (index == 0) {
			this->head = this->head->next;
			return;
		}

		ListNode *pre = this->head;
		for (int i = 0; i < index; i++) pre = pre->next;
		if (index == this->size() - 1)
		{
			pre->next = nullptr;
			return;
		}
		ListNode *cur = pre->next;
		//排除對空指針的引用
		if (pre->next) pre->next = cur->next;
		delete cur;

	}

	int size()
	{
		int count = 0;
		ListNode *cur = this->head;
		while (cur->next)
		{
			count += 1;
			cur = cur->next;
		}
		
		return count;
	}

	ListNode *head;
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章