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 ...
随机推荐
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Jquery轻量级插件--操作URL
调用: > "?action=view§ion=info&id=123&debug&testy[]=true&testy[]=false ...
- Java-Class-@I:io.swagger.annotation.Api
ylbtech-Java-Class-@I:io.swagger.annotation.Api 1.返回顶部 2.返回顶部 1. package com.ylbtech.api.controlle ...
- iptables防DDOS攻击和CC攻击配置
防范DDOS攻击脚本 #防止SYN攻击 轻量级预防 iptables -N syn-flood iptables -A INPUT -p tcp –syn -j syn-flood iptables ...
- leetcode.数组.16最接近的三数之和-java
1. 具体题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案 ...
- 集合 HashMap 的原理,与 Hashtable、ConcurrentHashMap 的区别
一.HashMap 的原理 1.HashMap简介 简单来讲,HashMap底层是由数组+链表的形式实现,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表 ...
- .net core 根据环境变量区分正式、测试 配置文件
新建测试.正式环境下所用的 配置信息文件 appsettings.Development.json 文件信息: { "Logging": { "LogLevel" ...
- idea一直在copying resources或者一直在build,卡住不动,问题已解决
IDE在启动项目的过程中,一直copying,或者是build一直卡住,需要在modules中配置下,把target文件夹移出resource folder 同时,在此之前,我还屏蔽了项目中pom.x ...
- 【学术篇】CF935E Fafa and Ancient Mathematics 树形dp
前言 这是一道cf的比赛题.. 比赛的时候C题因为自己加了一个很显然不对的特判WA了7次但找不出原因就弃疗了... 然后就想划水, 但是只做了AB又不太好... 估计rating会掉惨 (然而事实证明 ...
- macro `AM_PROG_LIBTOOL’ not found in library
https://blog.csdn.net/yongcai1/article/details/8693068 yum install libtool