leetcode 707——设计链表

leetcode 707——设计链表

题目描述:

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
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

提示:

所有val值都在 [1, 1000] 之内。
操作次数将在 [1, 1000] 之内。
请不要使用内置的 LinkedList 库。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

提交:

在这里插入图片描述

class MyLinkedList {
public:
	/** Initialize your data structure here. */
	MyLinkedList() :next(nullptr), size(0) {}
	MyLinkedList(int val) :val(val), next(nullptr), size(0) {}

	/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
	int get(int index) {//index : 0 1 2 …… size-1
		if (index < 0 || index > size - 1) return -1;
		MyLinkedList* pMove = this->next;
		for (int i = 0; i < index; i++)
		{
			pMove = pMove->next;
		}
		return pMove->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) {
		MyLinkedList* newNode = new MyLinkedList(val);
		newNode->next = this->next;
		this->next = newNode;
		++this->size;
	}

	/** Append a node of value val to the last element of the linked list. */
	void addAtTail(int val) {
		MyLinkedList* newNode = new MyLinkedList(val);
		MyLinkedList* pMove = this;
		while (pMove->next) {
			pMove = pMove->next;
		}
		pMove->next = newNode;
		++this->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) {//index : 0 1 2 …… size-1
		if (index == size) addAtTail(val);
		else if (index > size) return;
		else if (index <= 0) addAtHead(val);
		else {//1 2 3 size-1
			MyLinkedList* frontPosNode = this;
			MyLinkedList* posNode = this->next;
			while (index--) {
				frontPosNode = posNode;
				posNode = posNode->next;
			}
			MyLinkedList* newNode = new MyLinkedList(val);
			frontPosNode->next = newNode;
			newNode->next = posNode;
            ++this->size;
		}
	}

	/** Delete the index-th node in the linked list, if the index is valid. */
	void deleteAtIndex(int index) {//index : 0 1 2 …… size-1
		if (index<0 || index>size - 1) return;
		if (index == 0)
		{//删除头结点
			MyLinkedList* headNode = this->next;
			MyLinkedList* nextNode = headNode->next;
			delete headNode;
			this->next = nextNode;
		}
		else
		{
			MyLinkedList* frontPosNode = this;
			MyLinkedList* posNode = this->next;
			while (index--) {
				frontPosNode = posNode;
				posNode = posNode->next;
			}
			frontPosNode->next = posNode->next;
			delete posNode;
		}
		this->size--;
	}
public:
	int val;
	MyLinkedList* next;
	int size;
};

发布了68 篇原创文章 · 获赞 12 · 访问量 3533

猜你喜欢

转载自blog.csdn.net/weixin_44795839/article/details/104256761