设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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. symbolicatecrash解析crash文件

    导出crash文件 Xcode -> Window -> Devices and Simulators -> View Device Logs ,然后选中导出. 找到.app文件和. ...

  2. 吴裕雄--天生自然 R语言开发学习:广义线性模型

    #----------------------------------------------# # R in Action (2nd ed): Chapter 13 # # Generalized ...

  3. angular jspaf

    import { Component, OnInit } from '@angular/core'; import * as jsPDF from 'jspdf'; import html2canva ...

  4. Java Timer和TimerTask

    Timer是JDK中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的任务,可以指定一次或多次. TimerTask是一个实现了Runnable接口的抽象类,代表一个可被执行的任 ...

  5. 斐波那契数列的第N项

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242 题目: 斐波那契数列的定义如下:   F(0) = 0 ...

  6. 两种HTTP请求方法:GET和POST的区别

    之前在一些开发者平台使用网页调用API时,一再提到两种请求方法GET和POST,所以就去了解了下.那么这又不得不提到HTTP了! 一.什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户 ...

  7. vue项目实战

    本篇文章主要介绍了vue的环境配置,vue项目的目录结构以及在开发vue项目中问题的一些解决方案. 环境配置及目录结构 1.安装node.js(http://www.runoob.com/nodejs ...

  8. 安卓权威编程指南-笔记(第27章 broadcast intent)

    本章需求:首先,让应用轮询新结果并在有所发现时及时通知用户,即使用户重启设备后还没有打开过应用.其次,保证用户在使用应用时不出现新结果通知. 1. 一般intent和broadcast intent ...

  9. 4款java快速开发平台推荐

    JBoss Seam JBoss Seam,算得上是Java开源框架里面最优秀的快速开发框架之一. Seam框架非常出色,尤其是他的组件机制设计的很有匠心,真不愧是Gavin King精心打造的框架了 ...

  10. 从头认识js-js的发展历史

    JavaScript简介 JavaScript诞生于1995年,当时,它的主要目的是处理以前有服务端语言(如Perl)负责的一些输入验证操作. JavaScript简史 1995年2月当时就职于Net ...