Skip to content

Instantly share code, notes, and snippets.

@Se7soz
Last active January 3, 2016 05:18
Show Gist options
  • Select an option

  • Save Se7soz/8414357 to your computer and use it in GitHub Desktop.

Select an option

Save Se7soz/8414357 to your computer and use it in GitHub Desktop.
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
void remove(node* nd) {
if(nd == NULL || nd->nxt == NULL) return; // It should be in the middle
node* lnk1 = nd, *lnk2 = nd->nxt;
while(lnk2->nxt) {
swap(lnk1->d, lnk2->d);
lnk1 = lnk1->nxt;
lnk2 = lnk2->nxt;
}
swap(lnk1->d, lnk2->d);
lnk1->nxt = NULL;
delete lnk2;
}
/* a better solution, thanks to Manal Sadek */
void remove(node* nd) {
if(nd == NULL || nd->nxt == NULL) return; // It should be in the middle
node* lnk1 = nd, *lnk2 = nd->nxt;
swap(lnk1->d, lnk2->d);
lnk1->nxt = lnk2->nxt;
delete lnk2;
}
@ManalSadek

Copy link
Copy Markdown

Why u loop till then end of the list and delete the last element
what about this one

void remove(node* nd) {
 
if(nd == NULL || nd->nxt == NULL) return; // It should be in the middle
 
node* lnk1 = nd, *lnk2 = nd->nxt;

swap(lnk1->d, lnk2->d);
lnk1->nxt = lnk2->nxt;
delete lnk2;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment