Skip to content

Instantly share code, notes, and snippets.

@iamvon
Created June 7, 2018 10:14
Show Gist options
  • Select an option

  • Save iamvon/ec0d47ae8bdc5464f93fb402fc5dbd97 to your computer and use it in GitHub Desktop.

Select an option

Save iamvon/ec0d47ae8bdc5464f93fb402fc5dbd97 to your computer and use it in GitHub Desktop.
Node* addNode(Node* head, int index, int value){
Node *addElement = new Node;
Node *pre = new Node;
Node *cur = new Node;
addElement->value = value;
addElement->nextNode = NULL;
if(head == NULL) head = addElement;
else {
cur = head;
for(int i = 0; i <= index; ++i) {
pre = cur;
cur = cur->nextNode;
}
pre->nextNode = addElement;
addElement->nextNode = cur;
}
return head;
}
Node* deleteNode(Node* head, int index){
Node *pre = new Node;
Node *cur = new Node;
cur = head;
if(head == NULL) return head;
else {
for(int i = 0; i < index; ++i) {
pre = cur;
cur = cur->nextNode;
}
pre->nextNode = cur->nextNode;
return head;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment