Last active
December 26, 2015 06:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void removeNode(int val, LinkedList **list) { | |
while (*list) { | |
if ((*list)->val == val) { | |
*list = (*list)->next; | |
} | |
else | |
list = &(*list)->next; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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