publicstaticclassMyLinkedList { int size; ListNode head;
publicMyLinkedList() { size = 0; head = newListNode(0); }
// 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. publicvoidaddAtIndex(int index, int val) {
//If index is greater than the length, // the node will not be inserted. if (index > size) { return; }
// [so weird] If index is negative, // the node will be inserted at the head of the list. if (index < 0) { index = 0; } ++size;
//find predecessor of the node to be added ListNodepre= head; for (inti=0; i < index; i++) { pre = pre.next; }
// node to be added ListNodetoAdd=newListNode(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. publicvoidaddAtHead(int val) { addAtIndex(0, val); }
// Append a node of value val to the last element of the linked list. publicvoidaddAtTail(int val) { addAtIndex(size, val); }
// Delete the index-th node in the linked list, if the index is valid. publicvoiddeleteAtIndex(int index) {
// if the index is invalid, do nothing if (index < 0 || index >= size) return;
size--;
// find predecessor of the node to be deleted ListNodepre= head; for(inti=0; i < index; ++i) { pre = pre.next; } // delete pre.next pre.next = pre.next.next; }
// Get the value of the index-th node in the linked list. If the index is invalid, return -1. publicintget(int index) { // if index is invalid if (index < 0 || index >= size) { return -1; }
ListNodecur= head; // index steps needed // to move from sentinel node to wanted index for (inti=0; i < index + 1; i++) { cur = cur.next; } return cur.val; } }
// choose the fastest way: to move from the head // or to move from the tail ListNodecurr= head; if (index + 1 < size - index) for(inti=0; i < index + 1; ++i) curr = curr.next; else { curr = tail; for(inti=0; i < size - index; ++i) curr = curr.prev; }
// Get the value of the index-th node in the linked list. If the index is invalid, return -1. publicintget(int index) { // if index is invalid if (index < 0 || index >= size) return -1;
// choose the fastest way: to move from the head // or to move from the tail DListNodecur= head; if (index + 1 < size - index) for(inti=0; i < index + 1; ++i) cur = cur.next; else { cur = tail; for(inti=0; i < size - index; ++i) cur = cur.pre; }
return cur.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. publicvoidaddAtHead(int val) { DListNodepre= head, succ = head.next;
// Append a node of value val to the last element of the linked list. publicvoidaddAtTail(int val) { DListNodesucc= tail, pre = tail.pre;
++size; DListNodetoAdd=newDListNode(val); toAdd.pre = pre; toAdd.next = succ; pre.next = toAdd; succ.pre = toAdd; } // 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. publicvoidaddAtIndex(int index, int val) { // If index is greater than the length, // the node will not be inserted. if (index > size) return;
// [so weird] If index is negative, // the node will be inserted at the head of the list. if (index < 0) index = 0;
// find predecessor and successor of the node to be added DListNode pre, succ; if (index < size - index) { pre = head; for(inti=0; i < index; ++i) pre = pre.next; succ = pre.next; } else { succ = tail; for (inti=0; i < size - index; ++i) succ = succ.pre; pre = succ.pre; }
// Delete the index-th node in the linked list, // if the index is valid. publicvoiddeleteAtIndex(int index) { // if the index is invalid, do nothing if (index < 0 || index >= size) return;
// find predecessor and successor of the node to be deleted DListNode pre, succ; if (index < size - index) { pre = head; for(inti=0; i < index; ++i) pre = pre.next; succ = pre.next.next; } else { succ = tail; for (inti=0; i < size - index - 1; ++i) succ = succ.pre; pre = succ.pre.pre; }