Пример #1
0
void SwapPairs::TestClass()
{
	ListNode *l1;
	l1=(ListNode*)malloc(sizeof(ListNode));

	int A1[4]={1,2,3,4};
	l1->val=A1[0];
	l1->next=NULL;

	ListNode *p;p=l1;
	for (int i=1;i<4;i++)
	{
		ListNode *res;
		res=(ListNode*)malloc(sizeof(ListNode));
		res->val=A1[i];
		res->next=NULL;
		p->next=res;
		p=p->next;
	}
	ListNode* res=swapPairs(l1);
	p=res;
	while (p!=NULL)
	{
		cout<<p->val<<endl;
		p=p->next;
	}
}
Пример #2
0
Файл: 24.c Проект: unasm/utils
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;
}
Пример #3
0
 ListNode* swapPairs(ListNode* head) {
     if(head == NULL || head->next == NULL) return head;
     auto t = head->next;
     head->next = swapPairs(head->next->next);
     t->next = head;
     return t;
 }
 ListNode* swapPairs(ListNode* head) {
     if (head == NULL || head->next == NULL) return head;
     ListNode* next = head->next, *nextNext = next->next;
     next->next = head;
     head->next = swapPairs(nextNext);
     return next;
 }
Пример #5
0
 ListNode* swapPairs(ListNode* head) {
     if(!head || head->next==NULL) return head;
     ListNode *then = head->next;
     head->next = swapPairs(head->next->next);
     then->next = head;
     return then;
 }
Пример #6
0
 ListNode *swapPairs(ListNode *head) {
     if (!head || !head->next) return head;
     ListNode *post = swapPairs(head->next->next);
     ListNode *newHead = head->next;
     newHead->next = head;
     head->next = post;
     return newHead;
 }
 ListNode *swapPairs(ListNode *head) {
     if (!head || !head->next) 
         return head;
     ListNode * n = head->next;
     head->next = swapPairs(n->next);
     n->next = head;
     return n;
 }
 ListNode *swapPairs(ListNode *head) {
     if (head == NULL || head -> next == NULL) return head;
     ListNode *nextPair = head -> next -> next;
     ListNode *newHead = head -> next;
     newHead -> next = head;
     head -> next = swapPairs(nextPair);
     return newHead;
 }
Пример #9
0
 ListNode* swapPairs(ListNode* head) {
     if(!head || !head->next) return head;
     
     ListNode* t = head->next;
     head->next = swapPairs(t->next);
     t->next = head;
     return t;
 }
 ListNode *swapPairs(ListNode *head) {
     if (head == nullptr || head->next == nullptr) return head;
     ListNode *last = head->next->next;
     ListNode *newhead = head->next;
     newhead->next = head;
     head->next = swapPairs(head->next->next);
     return newhead;
 }
Пример #11
0
 ListNode* swapPairs(ListNode* head) {
     if(!head || head->next == NULL)
         return head;
     ListNode *new_head = head->next;
     head->next = swapPairs(head->next->next);;
     new_head->next = head;
     return new_head;
 }
Пример #12
0
int main()
{
    vector<int> arr = {1,2,4,5,6,7,8};
    ListNode* data = CreateList(arr);
    PrintList(data);
    ListNode* ret = swapPairs(data);
    PrintList(ret);
    DestoryList(ret);
}
 ListNode* swapPairs(ListNode* head) {
     if(head == NULL || head->next == NULL) return head;
     
     ListNode *nextList = swapPairs(head->next->next);
     ListNode *tmp = head->next;
     head->next = nextList;
     tmp->next = head;
     return tmp;
 }
Пример #14
0
int main()
{
	ListNode *head, *head1;
	head = createList();
	printList(head); 
	head1 = swapPairs(head);
	printList(head1);
	return 0;
}
 ListNode* swapPairs(ListNode* head) {
     if ( head == nullptr || head->next == nullptr ) return head;
 
     ListNode* prev = head;
     ListNode* curr = head->next;
     prev->next = swapPairs(curr->next);
     curr->next = prev; 
     return curr;
 }
Пример #16
0
 ListNode *swapPairs(ListNode *head) {
     if(!(head != NULL && head->next != NULL)) {
         return head;
     }
     int temp = head->val;
     head->val = head->next->val;
     head->next->val = temp;
     swapPairs(head->next->next);
     return head;
 }
Пример #17
0
ListNode* swapPairs(ListNode* head) {
    if (head == NULL) { return NULL; }
    if (head ->next == NULL) { return head; }
    
    ListNode *nextNode = head->next;
    ListNode *node = head->next->next;
    nextNode->next = head;
    head->next = swapPairs(node);
    return nextNode;
}
Пример #18
0
void test(int test[], int n) {
    struct ListNode *list;
    create_list(test, n, &list);
    print_list(list);
    list = swapPairs(list);
    //list = swapPairs_by_ulyx(list);
    print_list(list);
    free_list(&list);
    printf("\n");
}
Пример #19
0
struct ListNode * swapPairs_by_ulyx(struct ListNode * head) {
    if (head && head->next) {
        struct ListNode * newhd = head->next;
        head->next = swapPairs(newhd->next);
        newhd->next = head;
        return newhd;
    } else {
        return head;
    }
}
Пример #20
0
int main()
{
	int set[] = {0, 1, 2, 3, 4, 5, 6};

	struct ListNode *list1 = initlist(set, sizeof(set)/sizeof(int));
	printlist(list1);

	struct ListNode *res = swapPairs(list1);
	printlist(res);
}	
Пример #21
0
 ListNode* swapPairs(ListNode* head) {
     ListNode *next = head;
     if (head && head->next) {
         ListNode *temp = head->next->next;
         head->next->next = head;
         next = head->next;
         head->next = swapPairs(temp);
     }
     return next;
 }
Пример #22
0
int main(){
    int arr[] = {1, 2, 3, 4, 5};
    ListNode* l = create_list(arr, sizeof(arr)/sizeof(arr[0]));
    ListNode* r = swapPairs(l);
    while(r){
        printf("%d ", r->val);
        r = r->next;
    }
    printf("\n");
    return 0;
}
Пример #23
0
//This kind of questions can be done using recursive also
ListNode *swapPairs(ListNode *head) {
	if(head == NULL || head->next == NULL) return head;
	ListNode * curr = head;
	ListNode * tmp = NULL;
	if(curr->next && curr->next->next){
		tmp = swapPairs(head->next->next);
	}
	ListNode * nextone = curr->next;
	curr->next = tmp;
	nextone->next = curr;
	return nextone;
}
Пример #24
0
 ListNode *swapPairs(ListNode *head) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     // Recursively call the function to deal with first two nodes
     if(!head || !head->next) return head;
     ListNode *nextNode = head->next;
     head->next = nextNode->next;
     nextNode->next = head;
     head = nextNode;
     head->next->next = swapPairs(head->next->next);
     return head;
 }
Пример #25
0
  // Recursive solution
  ListNode *swapPairs2(ListNode* head) {
    if (! head || ! head->next) {
      return head;
    }

    ListNode* fst = head, *snd = head->next;
    fst->next = snd->next;
    snd->next = fst;
    fst->next = swapPairs(fst->next);

    return snd;
  }
Пример #26
0
 ListNode *swapPairs(ListNode *head) {
     if(head==NULL || head->next==NULL)
         return head;
     ListNode dummy(-1);
     dummy.next=head;
     ListNode* p=head;
     remainhead=p->next->next;//p->next!=NULL, length>=2
     dummy.next=p->next;
     p->next->next=p;
     p->next=swapPairs(remainhead);
     return dummy.next;
 }
Пример #27
0
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head)
{
    if (head == NULL)
        return NULL;
    if (head->next == NULL)
        return head;

    struct ListNode* p = head->next;
    struct ListNode* q = swapPairs(p->next);
    p->next = head;
    head->next = q;
    return p;
}
Пример #28
0
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head) {
    
    if (!head || !head->next) 
        return head;
        
    struct ListNode* second = head->next;
    struct ListNode* third = second->next;
   
    second->next = head;
    head->next = swapPairs(third);
    
    return second;
}
Пример #29
0
	void test()
	{
		ListNode *list = new ListNode(1);
		list->next = new ListNode(2);
		list->next->next = new ListNode(3);

		ListNode *res = swapPairs(list);
		while(res)
		{
			cout << res->val << endl;
			res = res->next;
		}
	}
Пример #30
0
    ListNode* swapPairs(ListNode* head) {
        if (head == NULL || head->next == NULL)
            return head;

        ListNode* newHead = head->next;
        ListNode *curNode = head, *tmpNode;

        if (curNode != NULL && curNode->next != NULL) {
            tmpNode = curNode->next;
            curNode->next = swapPairs(tmpNode->next);
            tmpNode->next = curNode;
        }

        return newHead;
    }