/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <iostream>
#include <stdio.h>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val; //指数
// int coef; //系数
ListNode *next;
ListNode(int x) : val(x),next(NULL) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* p1=head;
ListNode* p2=NULL;
ListNode* temp=NULL;
//check null list
if(head==NULL) return head;
//check single-node list;
if(head->next==NULL) return head;
head=p1->next;
temp=p1;
int flag=0;
while(p1!=NULL&&p1->next!=NULL){
p2=p1->next;
p1->next=p2->next;
p2->next=p1;
flag++;
//head=p2;
if(flag>=2)
{
temp->next=p2;
temp=p1;
}//暂时变量,可以改变位置 相当于对P1和P3重新接后续链表
p1=p1->next;
}
// while(p1!=NULL&&p1->next!=NULL);
return head;
}
};
int main() {
Solution solution;
//int A[] = {2,4,7,9};
//int B[] = {5,6,4};
int i;
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 1;i <10;i++){
node = new ListNode(i);
// node->exp = A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
/* pre = head2;
for(int i = 2;i < 5;i++){
node = new ListNode(i,i+1);
// node->val = B[i];
node->next = NULL;
pre->next = node;
pre = node;
}*/
head = solution.swapPairs(head1->next);
while(head != NULL){
// printf("%d %d ",head->coef,head->exp);
cout<<head->val<<' ';
head = head->next;
}
//i=(head1->val);
//cout<<i;
return 0;
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <iostream>
#include <stdio.h>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val; //指数
// int coef; //系数
ListNode *next;
ListNode(int x) : val(x),next(NULL) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* p1=head;
ListNode* p2=NULL;
ListNode* temp=NULL;
//check null list
if(head==NULL) return head;
//check single-node list;
if(head->next==NULL) return head;
head=p1->next;
temp=p1;
int flag=0;
while(p1!=NULL&&p1->next!=NULL){
p2=p1->next;
p1->next=p2->next;
p2->next=p1;
flag++;
//head=p2;
if(flag>=2)
{
temp->next=p2;
temp=p1;
}//暂时变量,可以改变位置 相当于对P1和P3重新接后续链表
p1=p1->next;
}
// while(p1!=NULL&&p1->next!=NULL);
return head;
}
};
int main() {
Solution solution;
//int A[] = {2,4,7,9};
//int B[] = {5,6,4};
int i;
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 1;i <10;i++){
node = new ListNode(i);
// node->exp = A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
/* pre = head2;
for(int i = 2;i < 5;i++){
node = new ListNode(i,i+1);
// node->val = B[i];
node->next = NULL;
pre->next = node;
pre = node;
}*/
head = solution.swapPairs(head1->next);
while(head != NULL){
// printf("%d %d ",head->coef,head->exp);
cout<<head->val<<' ';
head = head->next;
}
//i=(head1->val);
//cout<<i;
return 0;
}
真的很恶心的一道题,关键是理解的不到位,temp是变量,可以指向任意节点,但是对temp的后续节点改变会影响P!的后续。 主要是先让它跑一次,再来比较。