菜鸟刷leetcode 24. Swap Nodes in Pairs
来源:互联网 发布:java phantomjs pdf 编辑:程序博客网 时间:2024/06/07 04:39
自己写的时候,想到了交换的方法,但是没有想到前面的点没有前继,导致[1 2 3 4 5]只出现结果[1 3 5]。当时是这样写的:
while()
{
p=head->next;
head->next=p->next;
p->next=head;
head=p->next->next;
}
由于p前面没有数据,导致消失。应该加一个前继,不然链表断了。
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode dummy(0),*pre=&dummy,*left(nullptr),*right(nullptr);
dummy.next=head;
while(pre!=NULL&&pre->next!=NULL&&pre->next->next!=NULL)
{
left=pre->next;
right=left->next;
left->next=right->next;
pre->next=right;
right->next=left;
pre=pre->next->next;
}
return dummy.next;
}
};
0 0
- 菜鸟刷leetcode 24. Swap Nodes in Pairs
- [LeetCode]24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs Leetcode Python
- LeetCode --- 24. Swap Nodes in Pairs
- LeetCode 24.Swap Nodes in Pairs
- [Leetcode] 24. Swap Nodes in Pairs
- 【LeetCode】24.Swap Nodes in Pairs
- [leetcode] 24.Swap Nodes in Pairs
- **LeetCode 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs LeetCode
- leetcode 24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- LeetCode--24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- 【LeetCode】24. Swap Nodes in Pairs
- [leetcode] 24. Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- LeetCode-24.Swap Nodes in Pairs
- 使用百度静态资源库bootstrap字体出现问题
- xutils发送字组格式的数据(典数数组数据)
- 8位位图调色板相关图像处理
- 【Android】【内存】内存泄露检查工具 LeakCanary
- 流 java 流 字符流 字节 流的学习与理解
- 菜鸟刷leetcode 24. Swap Nodes in Pairs
- 结构体 struct 和 类Class 区别
- 清除WKWebView cookies
- 创建表增删改查
- JavaScript全选、全不选、反选小练习
- 人力资源系统中的绩效模块流程
- java弃用一个方法的注解
- ARM官方《CMSIS-RTOS教程》之线程的管理和优先级Thread Management and Priority
- Qt绘图 使用QWT绘制科学图表、绘图