Skip to content

Instantly share code, notes, and snippets.

@cli248
Last active December 26, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cli248/7106064 to your computer and use it in GitHub Desktop.
Save cli248/7106064 to your computer and use it in GitHub Desktop.
Double Pointer Gists: (1) Using double pointer to insert node in a sorted linked list; (2) Using double pointer to remove all elements from a linked list that have value N (3) Using double pointer to remove duplicates from sorted linked list
void insertNode(LinkedList *newIp, LinkedList *list) {
LinkedList **lpp;
LInkedList *lp;
for (lpp = &list; *lpp != NULL; lpp = &(*lpp)->next) {
lp = *lpp;
if (newIp->val < lp->val) {
newIp->next = lp;
*lpp = newIp;
break;
}
}
if (*lpp == NULL)
*lpp = newIp;
}
void removeNode(int val, LinkedList **list) {
while (*list) {
if ((*list)->val == val) {
*list = (*list)->next;
}
else
list = &(*list)->next;
}
}
LinkedList *deleteDuplicates(ListNode *head) {
ListNode **lpp = &head;
int currValue = INT_MAX;
while (*lpp) {
if ((*lpp)->val == currValue) {
*lpp = (*lpp)->next;
}
else if ((*lpp)->next && (*lpp)->val == (*lpp)->next->val) {
currValue = (*lpp)->val;
*lpp = (*lpp)->next;
}
else
lpp = &(*lpp)->next;
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment