[24]Swap Nodes in Pairs
来源:互联网 发布:韩国作曲家甘雨知乎 编辑:程序博客网 时间:2024/06/12 23:59
【题目描述】
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
【思路】就是将两个两个的节点调换位置,要注意的是要判断该节点是头结点
【代码】
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* swapPairs(ListNode* head) { if(head==NULL||head->next==NULL) return head; ListNode* node=head; ListNode *a; ListNode *pre=NULL; while(node&&node->next){ a=node->next; node->next=a->next; a->next=node; if(pre){ pre->next = a; } else{ head=a; } pre=node; node=node->next; } return head; }};
0 0
- [24]Swap Nodes in Pairs
- 24、Swap Nodes in Pairs
- 24 Swap Nodes in Pairs
- #24 Swap Nodes in Pairs
- 24 Swap Nodes in Pairs
- 24 - Swap Nodes in Pairs
- 24 Swap Nodes in Pairs
- [24]Swap Nodes in Pairs
- [24] Swap Nodes in Pairs
- 【24】Swap Nodes in Pairs
- 24-Swap Nodes In Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes In Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- pga_aggregate_target和_pga_max_size都不能绝对限制实际PGA的使用
- hadoop 分布式计算开源框架 学习总结
- ios返回圆形图片
- AndroidStudio基础教程
- Android开发手记一 NDK编程实例
- [24]Swap Nodes in Pairs
- bzoj1072: [SCOI2007]排列perm
- HDU3584 Cube
- 栈的基本操作(顺序表)
- iOS JSON的选项NSJSONReadingMutableLeaves
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest I题(模拟)
- 为经典版eclipse增加web and JavaEE插件
- Gson解析原理概述
- 一看就会Android之SQLite中事务的使用