Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 18, 2015 10:28
Show Gist options
  • Save rohith2506/0d2f0f6e57eae3f6bcf1 to your computer and use it in GitHub Desktop.
Save rohith2506/0d2f0f6e57eae3f6bcf1 to your computer and use it in GitHub Desktop.
swap pairwise elements in linked list
void swap_pair(node *head){
if(head == NULL || head -> next == NULL) return ;
node *cur,*prev,*next;
cur = head -> next;
prev = head;
head = cur -> next;
while(true) {
next = cur -> next;
if(next == NULL || next -> next == NULL){
prev -> next = next;
break;
}
cur -> next = prev;
prev -> next = next -> next;
prev = cur;
cur = prev -> next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment