-
Notifications
You must be signed in to change notification settings - Fork 1
/
24.c
85 lines (79 loc) · 1.47 KB
/
24.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*************************************************************************
* File Name : 21.c
* Author : unasm
* Mail : unasm@sina.cn
* Last_Modified: 2015-05-07 11:07:31
************************************************************************/
#include<stdio.h>
#include <stdlib.h>
struct ListNode{
int val;
struct ListNode *next;
};
struct heap{
int val,pos;
}heap[10000];
/**
* * Definition for singly-linked list.
* * struct ListNode {
* * int val;
* * struct ListNode *next;
* };
*/
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
struct ListNode* swapPairs(struct ListNode* head) {
struct ListNode *l1, *p;
int cnt = 0,tmp;
p = head;
while(p != NULL) {
cnt++;
if(cnt == 2) {
cnt = 0;
swap(&l1->val, &p->val);
/*
tmp = l1->val;
l1->val = p->val;
p->val = tmp;
*/
}
if(cnt == 1) {
l1 = p;
}
p = p->next;
}
return head;
}
int main(int argc, const char *argv[])
{
struct ListNode *arr,*p, *p1;
p1 = (struct ListNode * )malloc(sizeof(struct ListNode));
p1->val = 39;
p1->next = NULL;
arr = p1;
p1 = (struct ListNode * )malloc(sizeof(struct ListNode));
p1->val = 239;
p1->next = NULL;
arr->next = p1;
/*
for(int i = 0;i < 2;i++) {
p = arr[i];
printf("\n");
while(p != NULL) {
//printf("sdfadsf\n");
//printf("%d\n" , p->val);
p = p->next;
}
}
*/
p = swapPairs(arr);
while(p != NULL) {
printf("%d\t", p->val);
p = p->next;
}
printf("\n");
return 0;
}