Skip to content

Instantly share code, notes, and snippets.

@ygabo
Created August 20, 2013 17:29
Show Gist options
  • Save ygabo/6284548 to your computer and use it in GitHub Desktop.
Save ygabo/6284548 to your computer and use it in GitHub Desktop.
Interweave a linked list into another list.
int main(){
Node a(1), b(2), c(3);
Node d(4), e(5), f(6);
a.next = &c;
//b.next = &c;
d.next = &e; e.next = &f;
Node *temp = &a, *curr;
Node *next = &d;
while(temp){
curr = temp;
temp = curr->next;
if( next ){
curr->next = next;
next = next->next;
curr->next->next = temp;
}
else
break;
}
temp = &a;
while( temp ){
cout << temp->d << " ";
temp = temp->next;
}
temp = &a;
while( temp ){
cout << temp->d << " ";
temp = temp->next;
}
cout << endl <<"done" <<endl;
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment