[LeetCode]Swap Nodes in Pairs 交换节点
来源:互联网 发布:阿里云如何查看发票 编辑:程序博客网 时间:2024/05/21 17:07
/**
* 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!的后续。 主要是先让它跑一次,再来比较。
0 0
- [LeetCode]Swap Nodes in Pairs 交换节点
- Swap Nodes in Pairs 链表两两交换节点@LeetCode
- LeetCode-24 Swap Nodes in Pairs(链表中按要求交换节点)
- LeetCode 24. Swap Nodes in Pairs(交换节点)
- LeetCode 24. Swap Nodes in Pairs ***** 双节点交换
- [LeetCode] Swap Nodes in Pairs(成对交换节点)
- [LeetCode]Swap Nodes in Pairs 成对交换
- LeetCode Swap Nodes in Pairs交换链表中的两个节点
- [C++]LeetCode: 109 Swap Nodes in Pairs (交换相邻节点位置)
- Leetcode Swap Nodes in Pairs 交换链表的节点对
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
- Leetcode #24. Swap Nodes in Pairs 链表节点交换 解题报告
- 【链表&交换邻节点】Swap Nodes in Pairs
- Swap Nodes in Pairs 链表交换相邻的节点
- LeetCode24 Swap Nodes in Pairs 成对交换链表节点
- 24. Swap Nodes in Pairs-节点交换|指针
- Swap Nodes in Pairs 交换LinkList的相邻节点
- (LeetCode)Swap Nodes in Pairs --- 交换两个相邻的结点
- hdu D-City 反向并查集
- sdut-2156(模拟,bfs)
- 传智播客 Html基础知识学习笔记
- OpenCV图像变换(一)卷积
- 从无到有,质的变化
- [LeetCode]Swap Nodes in Pairs 交换节点
- Anroid内存使用研究【转】
- linux 下oracle 开机自启动命令(二)
- IoCancelIrp源码简析
- Android IOS WebRTC 音视频开发
- Plus One python 题解
- 聪明的kk
- HTML基础学习笔记
- jQuery实现锁屏