Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and nextval is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(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.
  • addAtTail(val) : Append a node of value val to the last element of the linked list.
  • addAtIndex(index, val) : 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. If index is negative, the node will be inserted at the head of the list.
  • deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
linkedList.get(1); // returns 2
linkedList.deleteAtIndex(1); // now the linked list is 1->3
linkedList.get(1);    // returns 3 注意一下corner case,一定先要问清楚corner case的处理。
 class MyLinkedList {
class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
}
}
private Node head;
private int size; /** Initialize your data structure here. */
public MyLinkedList() { } /** 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 >= size || index < ) return -;
Node current = head;
for (int i = ; i < index; i++) {
current = current.next;
}
return current.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. */
public void addAtHead(int val) {
Node node = new Node(val);
node.next = head;
head = node;
size++;
} /** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
Node node = new Node(val);
size++;
if (head == null) {
head = node;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.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 > size) return;
if (index <= ) {
addAtHead(val);
} else {
size++;
Node current = head;
for (int i = ; i < index - ; i++) {
current = current.next;
}
Node node = new Node(val);
node.next = current.next;
current.next = node;
}
} /** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index >= size || index < ) return;
size--;
if (index == ) {
head = head.next;
} else {
Node current = head;
for (int i = ; i < index - ; i++) {
current = current.next;
}
current.next = current.next.next;
}
}
}
												

最新文章

  1. 仅此一文让你明白ASP.NET MVC原理
  2. jQuery.makeArray() 函数详解
  3. 怒刷DP之 HDU 1029
  4. 语义Web和本体开发相关技术
  5. [转]各种字符串Hash函数比较
  6. nutch http file 截断问题
  7. 三种方法为QLineEdit添加清除内容按钮
  8. C++中字符编码的转换(Unicode、UTF-8、ANSI)
  9. BZOJ-8-2115: [Wc2011] Xor
  10. 【转载】Selenium WebDriver的简单操作说明
  11. [原][osgEarth]在osgearth中添加相机路径动画
  12. 20165310_Exp2实验二《Java面向对象程序设计》
  13. PostgreSQL教程收集(中文文档/命令行工具/常用命令)
  14. QWebView_QWebEngineView
  15. Code Forces Bear and Forgotten Tree 3 639B
  16. Python中返回SQL字段名
  17. ubuntu 致命错误: zlib.h:没有那个文件或目录【转】
  18. InfiniBand技术和协议架构分析
  19. Webpack-dashboard 简单使用
  20. 通过java api提交自定义hadoop 作业

热门文章

  1. POJ1961 Period &amp;&amp; POJ2604 Power Strings 字符串循环节
  2. 洛谷 U5122 T2-power of 2(费马小定理)
  3. 在ABP core中使用RabbitMq
  4. mybatis之&lt;trim
  5. ps 证件照制作
  6. [JDBC]批量提交插入语句以提高数据插入速度(效率提升不明显)
  7. jenkins 基于角色的权限管理
  8. dts是如何来描述iommu与PCI(e)之间的关系?
  9. [Java读书笔记] Effective Java(Third Edition) 第 3 章 对于所有对象都通用的方法
  10. python之NLP数据清洗