设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val nextval 是当前节点的值,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 库。

Code

struct myListNode
{
	int val;
	myListNode* next;
	myListNode() :val(-1), next(nullptr) {}
};

class MyLinkedList {
public:
	int m_size;
	myListNode* m_head;
	myListNode* m_tail;

public:
	/** Initialize your data structure here. */
	MyLinkedList() :m_size(0)
	{
		m_head = new myListNode();
	}

	/** 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 > m_size - 1 || index < 0)
		{
			return -1;
		}
		myListNode* p = m_head->next;
		while (index != 0)
		{
			p = p->next;
			--index;
		}
		//std::cout << p->val;
		return p->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* newNode = new myListNode();
		newNode->next = m_head->next;
		m_head->next = newNode;
		newNode->val = val;
		m_size += 1;
	}

	/** Append a node of value val to the last element of the linked list. */
	void addAtTail(int val)
	{
		myListNode* lastNode = new myListNode();
		lastNode->val = val;
		myListNode* p = m_head;
		int len = m_size;
		while (len != 0)
		{
			p = p->next;
			--len;
		}
		p->next = lastNode;
		lastNode->next = NULL;
		m_size += 1;
	}

	/** 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 > m_size) return;
		if (index == m_size)
		{
			addAtTail(val);
			return;
		}
		if (index < 0)
		{
			addAtHead(val);
			return;
		}
		myListNode* newNode = new myListNode();
		myListNode* p = m_head;
		while (index != 0)
		{
			p = p->next;
			--index;
		}
		newNode->next = p->next;
		p->next = newNode;
		newNode->val = val;
		m_size += 1;
	}

	/** Delete the index-th node in the linked list, if the index is valid. */
	void deleteAtIndex(int index)
	{
		if (index > m_size - 1 || index < 0) return;
		myListNode* p = m_head;
		while (index != 0)
		{
			p = p->next;
			--index;
		}
		myListNode* delNode = p->next;;
		p->next = p->next->next;
		m_size -= 1;
		delete delNode;
	}
};

LeetCode | 707. 设计链表的更多相关文章

  1. Java实现 LeetCode 707 设计链表(环形链表)

    707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...

  2. LeetCode——707 设计链表

    题目: 总而言之就是要用C++手撸链表,我的代码: class MyLinkedList { public: /** Initialize your data structure here. */ M ...

  3. LeetCode 707 ——设计链表

    1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...

  4. LeetCode——142 设计链表2

    题目 代码 class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head, *slow ...

  5. LeetCode——141 设计链表

    题目: 简单说下思路: 用两个指针,一个跑得快,一个跑得慢(例如一个每次前进两步,一个前进一步),这样只要快指针不会撞上NULL(如果遇到了NULL的情况那么必然不存在环),快指针肯定会和慢指针碰面( ...

  6. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

  7. 【LeetCode】Design Linked List(设计链表)

    这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...

  8. [Swift]LeetCode707. 设计链表 | Design Linked List

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  9. LeetCode707:设计链表 Design Linked List

    爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...

随机推荐

  1. PO设计模式-实现移动端自动化测试

    开发环境:python 3.6.5 + selenium 2.48.0 + pytest框架 + Android 5.1 工具:pycharm + Appium + Genymotion 测试机型:S ...

  2. npm安装依赖太慢问题

    执行 npm install 会发现很慢,可以在安装时手动指定从哪个镜像服务器获取资源,我使用的是阿里巴巴在国内的镜像服务器. 命令如下: npm install --registry=https:/ ...

  3. Servlet+JSP 对外访问路径配置

    servlet类似 servlet配置为: <servlet>    <servlet-name>Demo01_OutWrite</servlet-name>    ...

  4. Memcached笔记——(二)XMemcached&Spring集成

    今天研究Memcached的Java的Client,使用XMemcached 1.3.5,做个简单的测试,并介绍如何与Spring集成. 相关链接: Memcached笔记--(一)安装&常规 ...

  5. python基础修改haproxy配置文件

    1.通过eval(),可以将字符串转为字典类型. 2.Encode过程,是把python对象转换成json对象的一个过程,常用的两个函数是dumps和dump函数.两个函数的唯一区别就是dump把py ...

  6. MatterTrack Route Of Network Traffic :: Matter

    Python 1.1 基础 while语句 字符串边缘填充 列出文件夹中的指定文件类型 All Combinations For A List Of Objects Apply Operations ...

  7. Scrapy初体验(一) 环境部署

    系统选择centOs 7,Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 其最初是为了 页面抓取 (更确切来说, ...

  8. Image图片

    # View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...

  9. echarts饼图字体大小修改

    const option = { tooltip: { trigger: 'item', formatter: "{a} {b}: {c} ({d}%)" }, series: [ ...

  10. Swift 浅谈Struct与Class

    讨论Struct与Class之前,我们先来看一个概念:Value Type(值类型),Reference Type(引用类型): 1. 值类型的变量直接包含他们的数据,对于值类型都有他们自己的数据副本 ...