【Leetcode】Swap Nodes in Pairs
来源:互联网 发布:大数据平台运维管理 编辑:程序博客网 时间:2024/05/18 01:13
题目:
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.
解题思路:本题较简单,做3次指针变换就可以完成一轮交换。
代码:
/** * 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) { ListNode *Zero; ListNode *First=head,*Second; ListNode Forehead(0); if(head==NULL||head->next==NULL)return head; Zero=&Forehead; while(First!=NULL){ Second=First->next; if(Second!=NULL){ First->next=Second->next; Second->next=First; Zero->next=Second; Zero=First; First=First->next; }else{ break; } } return Forehead.next; }};
0 0
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- 【LeetCode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- Leetcode Swap Nodes in Pairs
- LeetCode Swap Nodes in Pairs
- 2014年移动互联网值得关注的四大领域
- DB2获得各种权限
- XML解析技术一
- 选择排序(Selection Sort)
- 为什么Spring MVC访问jsp页面报错
- 【Leetcode】Swap Nodes in Pairs
- 都怪殿下太花心
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- 【九度】题目1044:Pre-Post
- 一个Myeclipse中配置两个tomcat
- 调用EnableWindow禁止窗口接收输入
- spring源码解析
- HDU3047(带权并查集)
- Android中Message机制的灵活应用(一)