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.

如题实例所示,需要成对的交换节点,这里我用到了两个帮助节点,一个记下起点的前面位置,一个作为每一对prev的前面一个节点使用,思路比较简单,就是交换之后再向后面移动两个节点就可以了,代码如下所示:

 /**
* 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) return NULL;
if(!head->next) return head;
ListNode * helper1 = new ListNode(INT_MIN);
ListNode * helper2 = new ListNode(INT_MIN);
helper1->next = head;
helper2->next = head->next;// 先记录下首节点的位置,供函数返回的时候使用
ListNode * prev = head;
ListNode * curr = head->next;
while(prev){
if(curr){
ListNode * tmpNode = curr->next;
curr->next = prev;
helper1->next = curr;
prev->next = tmpNode;
helper1 = prev;
if(prev->next && prev->next->next){
prev = prev->next;
curr = prev->next;
}else
break;
}else
break; }
return helper2->next;
}
};

LeetCode OJ:Swap Nodes in Pairs(成对交换节点)的更多相关文章

  1. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  2. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  4. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  6. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  7. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  8. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  9. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  10. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

随机推荐

  1. Linux中Qt的安装

    1.下载Ot安装包 Qt5.30的下载地址如下,在网页中打开找到需要的资源,下载.run格式的安装软件. http://download.qt.io/archive/qt/5.3/5.3.0/qt-o ...

  2. RabbitMQ的安装及集群搭建方法

    RabbitMQ安装 1 安装erlang 下载地址:http://www.erlang.org/downloads 博主这里采用的是otp_src_19.1.tar.gz (200MB+) [roo ...

  3. Eclipse配置多个jdk

    Eclipse配置多个jdk 步骤: 1,打开windows > Preferences: 2,点击“Add”,新增jdk,选择“Standard VM”: 3,下一步,选择对应版本的jer: ...

  4. 阿里云 Linux 启用465端口发送邮件

    阿里云 Linux 启用465端口发送邮件 环境:阿里云 Linux Centos 7.4 x64 注:阿里云默认禁用25邮件端口,需要启动465端口加密进行邮件发送. 注:确保邮箱开启SMTP服务, ...

  5. hashmap总结2

    1.  关于HashMap的一些说法: a)  HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体.HashMap的底层结构是一个数组,数组中的每一项是一条链表. b)  Hash ...

  6. AD9361

    AD9361框图   1. Fir滤波器的阶数为64或128 而内插或抽取因子为:1.2或4. HB1和HB2的内插或抽取因子为1或2而HB3的因子为1.2或3 BB_LPF为:三阶巴特沃斯低通滤波器 ...

  7. git操作方便,简单使用的客户端sourcetree 安装配置所遇问题总结

    常言道:工欲善其事,必先利其器. SourceTree是老牌的Git GUI管理工具了,也号称是最好用的Git GUI工具 这里先言言它的好: * 免费 * 功能强大:无论你是新手还是重度用户,Sou ...

  8. Centos下ftp协议连接远程ftp server主机

    环境说明 [root@Check3 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@Check3 ~]# uname -a L ...

  9. Linq to SQL - 撤销所有未提交的改动

    在某些情况下我们需要撤销/丢弃所有未提交的改动,包括Update, Delete和Insert.context中GetChangeSet()方法可以返回当前所有未提交的改动,而我们的目标是清空Chan ...

  10. [BZOJ4137]火星商店问题

    Description 火星上的一条商业街里按照商店的编号1,2 ,…,n ,依次排列着n个商店.商店里出售的琳琅满目的商品中,每种商品都用一个非负整数val来标价.每个商店每天都有可能进一些新商品, ...