classMyLinkedList { public: /** Initialize your data structure here. */ MyLinkedList() { size = 0; head = nullptr; } /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ intget(int index){ if(index >= size || index < 0 || head == nullptr) { return-1; } SinglyListNode *temp = head; //从第一个节点开始 for (int i = 0;i < index; ++i) { temp = temp->next; //遍历查找 } return temp->val; //返回该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. */ voidaddAtHead(int val){ SinglyListNode *temp = new SinglyListNode(val); //new一个val值的结点 temp->next = head; //加在链表第一个元素之前 temp = nullptr; size++; } /** Append a node of value val to the last element of the linked list. */ voidaddAtTail(int val){ if (size == 0) { head = new SinglyListNode(val); //新建第一个结点 ++size; } SinglyListNode *find = head; while (find->next != nullptr) { find = find->next; //遍历到最后一个结点 }
SinglyListNode *temp = new SinglyListNode(val); find->next = temp; //最后一个节点的指针指向需要添加的这个元素 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. */ voidaddAtIndex(int index, int val){ if (index > size) return;
if (index <= 0) { addAtHead(val); //添加为第一个元素之前 return; }
SinglyListNode *pr = head; for (int i = 0;i < index-1; ++i) { pr = pr->next; } SinglyListNode *cur = new SinglyListNode(val); cur->next = pr->next; //插入到查找到的index前面 pr->next = cur; size++; } /** Delete the index-th node in the linked list, if the index is valid. */ voiddeleteAtIndex(int index){ if (index > size) return; if (index < 0) return;
if (index == 0) { head = head->next; size--; return; }
SinglyListNode *del = head; for (int i = 0;i < index-1; ++i) { del = del->next; } del->next = del->next->next; size--; }
private: structSinglyListNode { int val; SinglyListNode *next; SinglyListNode(int x) : val(x), next(nullptr) {} }; int size; SinglyListNode* head; };
/** * 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); */ //new对象,调用方法 MyLinkedList linkedList = new MyLinkedList(); linkedList.addAtHead(1); linkedList.addAtTail(3); linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3 linkedList.get(1); //返回2 linkedList.deleteAtIndex(1); //现在链表是1-> 3 linkedList.get(1); //返回3
C++的i++和++i
i++ :先引用后增加,先在i所在的表达式中使用i的当前值,后让i加1
++i :先增加后引用,让i先加1,然后在i所在的表达式中使用i的新值
即:
++i是将i的值先+1,然后返回i的值
i++是先将i的值存到寄存器里,然后执行i+1,然后返回寄存器里的值。
链表双指针模板(快慢指针java&cpp)
快慢指针可用于环形链表、相交链表的查询等运算
注意以下点:
在调用 next 字段之前,始终检查节点是否为空。获取空节点的下一个节点将导致空指针错误。例如,在我们运行fast = fast.next.next之前,需要检查fast和fast.next不为空
仔细定义循环的结束条件
C++语言版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Initialize slow & fast pointers ListNode* slow = head; ListNode* fast = head; /** * Change this condition to fit specific problem. * Attention: remember to avoid null-pointer error **/ while (slow && fast && fast->next) { slow = slow->next; // move slow pointer one step each time fast = fast->next->next; // move fast pointer two steps each time if (slow == fast) { // change this condition to fit specific problem returntrue; } } returnfalse; // change return value to fit specific problem
Java语言:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Initialize slow & fast pointers ListNode slow = head; ListNode fast = head; /** * Change this condition to fit specific problem. * Attention: remember to avoid null-pointer error **/ while (slow != null && fast != null && fast.next != null) { slow = slow.next; // move slow pointer one step each time fast = fast.next.next; // move fast pointer two steps each time if (slow == fast) { // change this condition to fit specific problem returntrue; } } returnfalse; // change return value to fit specific problem