LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List
题目让我们自己设计一个 linked list,可以是单向和双向的。这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code。
Java Solution:
Runtime: 56 ms, faster than 21.21%
Memory Usage: 45.2 MB, less than 88.89%
完成日期:07/08/2019
关键点:edge cases
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class MyLinkedList {
ListNode dummyHead;
/** Initialize your data structure here. */
public MyLinkedList() {
dummyHead = new ListNode(0);
dummyHead.next = null;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if(index < 0)
return -1;
ListNode cursor = dummyHead.next;
int i = 0;
while(cursor != null) {
if(i == index)
return cursor.val;
i++;
cursor = cursor.next;
}
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. */
public void addAtHead(int val) {
// case 1: no first node
if(dummyHead.next == null) {
ListNode node = new ListNode(val);
dummyHead.next = node;
node.next = null;
}
else {
ListNode node = new ListNode(val);
node.next = dummyHead.next;
dummyHead.next = node;
}
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
ListNode cursor = dummyHead;
// find the node's next is null, insert node after that node
while(cursor.next != null) {
cursor = cursor.next;
}
ListNode node = new ListNode(val);
node.next = cursor.next;
cursor.next = node;
}
/** 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. */
public void addAtIndex(int index, int val) {
if(index < 0) {
addAtHead(val);
return;
}
ListNode cursor = dummyHead;
ListNode prevNode = dummyHead;
int i = -1;
while(cursor != null) {
// if find the spot to insert node
if(i + 1 == index) {
ListNode node = new ListNode(val);
node.next = cursor.next;
cursor.next = node;
return;
}
i++;
cursor = cursor.next;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if(index < 0)
return;
ListNode cursor = dummyHead.next;
ListNode prevNode = dummyHead;
int i = 0;
while(cursor != null) {
if(i == index) { // find the node to delete
prevNode.next = cursor.next;
cursor.next = null;
return;
}
i++;
prevNode = cursor;
cursor = cursor.next;
}
}
}
/**
* 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);
*/
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 707. Design Linked List (设计链表)的更多相关文章
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- Leetcode707.Design Linked List设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- #Leetcode# 707. Design Linked List
https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【Leetcode_easy】707. Design Linked List
problem 707. Design Linked List 参考 1. Leetcode_easy_707. Design Linked List; 完
- 707. Design Linked List
1. 原始题目 Design your implementation of the linked list. You can choose to use the singly linked list ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- EcShop二次开发学习方法和Ecshop二次开发必备基础
ecshop二次开发学习方法 近年来,随着互联网的发展,电子商务也跟着一起成长,B2B,C2C,B2C的电子商务模式也不断的成熟.这时催生出了众多电子商务相关的php开源产品.B2C方面有Ecshop ...
- 【Web】Spring WebFlux
阅读目录 一.关于WebFlux 二.SpringMVC与SpringWebFlux 三.Reactive Spring Web 四.实现WebFlux示例 SpringWebflux是SpringF ...
- Yii2的一些问题
Yii2中删除能不能串着用 Yii2中find.findAll有什么区别 Yii2中User::findOne($id)和User::find->where(['id'=>1])-> ...
- thinkphp 配置参考
惯例配置 应用设定 'APP_USE_NAMESPACE' => true, // 应用类库是否使用命名空间 3.2.1新增 'APP_SUB_DOMAIN_DEPLOY' => fals ...
- luoguP1288 取数游戏II [博弈论]
题目描述 有一个取数的游戏.初始时,给出一个环,环上的每条边上都有一个非负整数.这些整数中至少有一个0.然后,将一枚硬币放在环上的一个节点上.两个玩家就是以这个放硬币的节点为起点开始这个游戏,两人轮流 ...
- TESTNG听录音笔记
1. 是什么:有了它可以管理测试用例,做数据驱动,多线程模式下case的鲍旭类型 2. 如何生成testng的xml文件 -- based on Eclipse Eclipse里装上testn插件,指 ...
- shellcode加密与解密
// Encoder.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...
- express简易代理请求
var express = require('express') var app = express() var proxy = require('http-proxy-middleware') va ...
- (转)OpenFire源码学习之一:XMPP基础知识
转:http://blog.csdn.net/huwenfeng_2011/article/details/43412919 前面两张主要讲基础部分.XMPP与Mina有部分抄写于互联网的其他大事 X ...
- python eval()内置函数
python有一个内置函数eval(),可以将字符串进行运行. 通过help(eval)查看帮助文档 Help on built-in function eval in module builtins ...