707. Design Linked List
https://leetcode.com/problems/design-linked-list/
js
function Node(val, next) {
this.val = val
this.next = next || null
}
/**
* Initialize your data structure here.
*/
var MyLinkedList = function() {
this.head = null
this.tail = null
this.size = 0
}
MyLinkedList.prototype.getNode = function(index) {
if (index === 0) {
return this.head
}
let node = this.head
for (let i = 1; i <= index; i++) {
node = node.next
}
return node
}
/**
* Get the value of the index-th node in the linked list. If the index is invalid, return -1.
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this.size) {
return -1
}
return this.getNode(index).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.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
this.head = new Node(val, this.head)
if (this.size === 0) {
this.tail = this.head
}
this.size++
}
/**
* Append a node of value val to the last element of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
let node = new Node(val, null)
if (this.size === 0) {
this.head = this.tail = node
} else {
this.tail.next = node
this.tail = node
}
this.size++
}
/**
* 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.
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
if (index < 0 || index > this.size) {
return
}
if (index === 0) {
return this.addAtHead(val)
}
if (index === this.size) {
return this.addAtTail(val)
}
prev = this.getNode(index - 1)
node = new Node(val, prev.next)
prev.next = node
this.size++
}
/**
* Delete the index-th node in the linked list, if the index is valid.
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this.size) {
return
}
if (index === 0) {
this.head = this.head.next
} else {
prev = this.getNode(index - 1)
prev.next = prev.next.next
if (index === this.size - 1) {
this.tail = prev
}
}
this.size--
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* var obj = Object.create(MyLinkedList).createNew()
* var param_1 = obj.get(index)
* obj.addAtHead(val)
* obj.addAtTail(val)
* obj.addAtIndex(index,val)
* obj.deleteAtIndex(index)
*/
py
class Node:
def __init__(self, val, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
"""
Initialize your data structure here.
"""
self.head = None
self.tail = None
self.size = 0
def getNode(self, index):
if index == 0:
return self.head
node = self.head
for i in range(1, index + 1):
node = node.next
return node
def get(self, index):
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
:type index: int
:rtype: int
"""
if index < 0 or index >= self.size:
return -1
return self.getNode(index).val
def addAtHead(self, 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.
:type val: int
:rtype: void
"""
self.head = Node(val, self.head)
if self.size == 0:
self.tail = self.head
self.size += 1
def addAtTail(self, val):
"""
Append a node of value val to the last element of the linked list.
:type val: int
:rtype: void
"""
node = Node(val)
if self.size == 0:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
self.size += 1
def addAtIndex(self, 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.
:type index: int
:type val: int
:rtype: void
"""
if index < 0 or index > self.size:
return
if index == 0:
return self.addAtHead(val)
if index == self.size:
return self.addAtTail(val)
prev = self.getNode(index - 1)
node = Node(val, prev.next)
prev.next = node
self.size += 1
def deleteAtIndex(self, index):
"""
Delete the index-th node in the linked list, if the index is valid.
:type index: int
:rtype: void
"""
if index < 0 or index >= self.size:
return
if index == 0:
self.head = self.head.next
else:
prev = self.getNode(index - 1)
prev.next = prev.next.next
if index == self.size - 1:
self.tail = prev
self.size -= 1
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)