#24. Swap Nodes in Pairs

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) {
ListNode* p=head;
ListNode* q=p;
while(p!=NULL&&p->next!=NULL)
{
ListNode* newlist=new ListNode(p->val);
p->val=p->next->val;
newlist->next=p->next->next;
p->next=newlist;
p=p->next->next;
}
return q;
}
};

Leetcode-24 Swap Nodes in Pairs的更多相关文章

  1. 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 ...

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

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

  3. [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 ...

  4. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

  5. Leetcode 24——Swap Nodes in Pairs

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

  6. 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 ...

  7. (链表 递归) 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 ...

  8. [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 ...

  9. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  10. [LeetCode] 24. Swap Nodes in Pairs ☆

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

随机推荐

  1. iOS Swift 数组 交换元素的两种方法

    swap(&arr[fromIndexPath.row], &arr[to.row]) (arr[fromIndexPath.row],arr[to.row]) = (arr[to.r ...

  2. mac apache 2.4的配置

    开启vhost 文件 命令行输入 vim /etc/apache2/httpd.conf 找到把前边的# include /private/etc/apache2/extra/httpd-vhost. ...

  3. jQuery.zTree的跳坑记录

    最近项目用到树型结构的交互,一开始并不打算选择zTree,为了项目进度我妥协了,这一妥协后果就是我进坑了,在2天的挣扎中,我终于跳出坑了,活了下来,有一些感慨纪录下来. 有一个业务场景需要2个树型结构 ...

  4. 迷之bug

    是这样的.要解决tbody滚动,而thead不动的布局问题,我把它们分别放在两个表格里,上面的只有thead,下面的只有tbody, 然后解决宽度对齐的问题,我用colgroup设置每列的宽度,用的都 ...

  5. 2632: [neerc2011]Gcd guessing game

    2632: [neerc2011]Gcd guessing game Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 144  Solved: 84[S ...

  6. 图文解释XCode常用快捷键的使用

    刚开始用Xcode是不是发现以前熟悉的开发环境的快捷键都不能用了?怎么快捷运行,停止,编辑等等.都不一样了.快速的掌握这些快捷键,能提供开发的效率. 其实快捷键在Xcode的工具栏里都标注有,只是有的 ...

  7. iphone中input标签会多出一块的解决办法

    -webkit-appearance: none;

  8. JAligner的一个坑

    JAligner是一个集成多个罚分矩阵的蛋白质序列比对工具包,提供充足的API供开发人员调用. 但是,不可否认的是,它的结构写得不够规范.以前我是将它放在普通的Java项目里使用,没有问题.但是,今天 ...

  9. Unity3d使用高通Vuforia发布IOS工程不支持64位的一些解决办法

    1.将Unit升级至4.6.x或5.0.x,将Vuforia差距升级到最新版本(vuforia-unity-mobile-android-ios-4-0-105 ) 2.平台Other Setting ...

  10. ES6(四) --- 正则 Number Math

    想学vue了  重启ES6的学习之路 在ES5 中正则的构造器  RegExp  不支持第二个参数 ES6 做了调整   第二个参数表示正则表达式的修饰符(flag) var regex = new ...