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 next. val 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.
deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.
题目分析:实现简单的单链表结构,并实现相应的操作功能:
- Get(获取链表中的某个位置位置元素)
- AddAtHead(在链表头插入元素)
- AddAtTail、AddAtIndex(在链表尾插入元素)
- DeleteAtIndex(在链表尾插入元素),
下面是基于go语言的实现:
type MyLinkedList struct {
Head *ListNode
Len int
}
/** Initialize your data structure here.*/
func Constructor() MyLinkedList {
return MyLinkedList{}
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1.*/
func (this *MyLinkedList) Get(index int) int {
if index < 0 || index >= this.Len {
return -1
}
currPoint := this.Head
for i := 0; i < index; i++ {
currPoint = currPoint.Next
}
return currPoint.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.*/
func (this *MyLinkedList) AddAtHead(val int) {
tmpNode := &ListNode{Val: val}
defer func() {
this.Len++
}()
if this.Len == 0 {
this.Head = tmpNode
return
}
tmpNode.Next = this.Head
this.Head = tmpNode
}
/** Append a node of value val to the last element of the linked list.*/
func (this *MyLinkedList) AddAtTail(val int) {
tmpNode := &ListNode{Val: val}
defer func() {
this.Len++
}()
if this.Len == 0 {
this.Head = tmpNode
return
}
currPoint := this.Head
for currPoint.Next != nil {
currPoint = currPoint.Next
}
currPoint.Next = tmpNode
}
/** 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.*/
func (this *MyLinkedList) AddAtIndex(index int, val int) {
if this.Len < index {
return
}
if index <= 0 {
this.AddAtHead(val)
return
}
currPoint := this.Head
for i := 0; i < index-1; i++ {
currPoint = currPoint.Next
}
currPoint.Next = &ListNode{Val: val, Next: currPoint.Next}
this.Len++
}
/** Delete the index-th node in the linked list, if the index is valid.*/
func (this *MyLinkedList) DeleteAtIndex(index int) {
if index >= this.Len || index < 0 {
return
}
defer func() {
this.Len--
}()
if index == 0 {
this.Head = this.Head.Next
return
}
currPoint := this.Head
for i := 0; i < index-1; i++ {
currPoint = currPoint.Next
}
currPoint.Next = currPoint.Next.Next
}