-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathSwapNodesInPairs.c
More file actions
44 lines (42 loc) · 968 Bytes
/
SwapNodesInPairs.c
File metadata and controls
44 lines (42 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
struct ListNode* swapPairs(struct ListNode* head){
int len=0;
struct ListNode * temp= head;
while(temp!= NULL){
len++;
temp=temp->next;
}
if(head==NULL){
return NULL;
}
if(len==1){
return head;
}
struct ListNode * ptr=head->next;
if(len==2){
ptr->next=head;
head->next=NULL;
return ptr;
}
struct ListNode *t=head;
struct ListNode *tt, *ttt;
if(len>2){
for(int i=0; i<len; i++){
if(i%2==0 && i==0){
tt=t->next;
t->next= tt->next;
tt->next=t;
}
if(i%2==0 && i!=len-1 && i>0){
tt=t->next;
ttt->next=t->next;
t->next= tt->next;
tt->next=t;
}
if(i%2!=0 ){
ttt=t;
t=t->next;
}
}
}
return ptr;
}