|
| 1 | +package _07_设计链表 |
| 2 | + |
| 3 | +type ListNode struct { |
| 4 | + Next *ListNode |
| 5 | + Val int |
| 6 | +} |
| 7 | + |
| 8 | +type MyLinkedList struct { |
| 9 | + head *ListNode |
| 10 | + size int |
| 11 | +} |
| 12 | + |
| 13 | +/** Initialize your data structure here. */ |
| 14 | +func Constructor() MyLinkedList { |
| 15 | + sentinel := &ListNode{Val: 0} |
| 16 | + return MyLinkedList{head: sentinel, size: 0} |
| 17 | +} |
| 18 | + |
| 19 | +/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ |
| 20 | +func (this *MyLinkedList) Get(index int) int { |
| 21 | + if index < 0 || index >= this.size { |
| 22 | + return -1 |
| 23 | + } |
| 24 | + cur := this.head |
| 25 | + |
| 26 | + var c int |
| 27 | + for cur.Next != nil && c <= index { |
| 28 | + c++ |
| 29 | + cur = cur.Next |
| 30 | + } |
| 31 | + |
| 32 | + return cur.Val |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +/** 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. */ |
| 37 | +func (this *MyLinkedList) AddAtHead(val int) { |
| 38 | + newNode := &ListNode{ |
| 39 | + Val: val, |
| 40 | + } |
| 41 | + newNode.Next = this.head.Next |
| 42 | + this.head.Next = newNode |
| 43 | + this.size++ |
| 44 | + return |
| 45 | +} |
| 46 | + |
| 47 | +/** Append a node of value val to the last element of the linked list. */ |
| 48 | +func (this *MyLinkedList) AddAtTail(val int) { |
| 49 | + newListNode := &ListNode{Val: val} |
| 50 | + cur := this.head |
| 51 | + for cur.Next != nil { |
| 52 | + cur = cur.Next |
| 53 | + } |
| 54 | + cur.Next = newListNode |
| 55 | + this.size++ |
| 56 | +} |
| 57 | + |
| 58 | +/** 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. */ |
| 59 | +func (this *MyLinkedList) AddAtIndex(index int, val int) { |
| 60 | + if index > this.size { |
| 61 | + return |
| 62 | + } |
| 63 | + newListNode := &ListNode{Val: val} |
| 64 | + cur := this.head |
| 65 | + var c int |
| 66 | + for cur.Next != nil && c < index { |
| 67 | + cur = cur.Next |
| 68 | + c++ |
| 69 | + } |
| 70 | + newListNode.Next = cur.Next |
| 71 | + cur.Next = newListNode |
| 72 | + this.size++ |
| 73 | +} |
| 74 | + |
| 75 | +/** Delete the index-th node in the linked list, if the index is valid. */ |
| 76 | +func (this *MyLinkedList) DeleteAtIndex(index int) { |
| 77 | + if index < 0 || index >= this.size { |
| 78 | + return |
| 79 | + } |
| 80 | + cur := this.head |
| 81 | + var c int |
| 82 | + for cur.Next != nil && c < index { |
| 83 | + cur = cur.Next |
| 84 | + c++ |
| 85 | + } |
| 86 | + cur.Next = cur.Next.Next |
| 87 | + this.size-- |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Your MyLinkedList object will be instantiated and called as such: |
| 92 | + * obj := Constructor(); |
| 93 | + * param_1 := obj.Get(index); |
| 94 | + * obj.AddAtHead(val); |
| 95 | + * obj.AddAtTail(val); |
| 96 | + * obj.AddAtIndex(index,val); |
| 97 | + * obj.DeleteAtIndex(index); |
| 98 | + */ |
0 commit comments