练习—单链表—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.
改变指针:
if (!head || !(head->next) ) return head;
ListNode *cur = head,*next = head->next;
head = head->next;
while(next){
next = next->next;//next->3
cur->next->next =cur;//2->1
if(!next || !(next->next)) {
cur->next = next;//1->3;
return head; }
cur->next = next->next;//否则 1->4
cur = next;// cur ->3
next = next->next;//next->4
}
return head;
只改变值:
ListNode* swapPairs(ListNode* head)
{
if(!head) return nullptr;
ListNode *frontPtr=head,*backPtr=head->next;
while(frontPtr && backPtr){ swap(frontPtr->val,backPtr->val);
frontPtr=frontPtr->next;
if(frontPtr!=nullptr)
frontPtr=frontPtr->next;
backPtr=backPtr->next;
if(backPtr!=nullptr)
backPtr=backPtr->next;
}
return head;
}
递归:
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL||head->next==NULL) return head;
ListNode* next = head->next;
head->next = swapPairs(next->next);
next->next = head;
return next;
}
};
练习—单链表—Swap Nodes in Pairs的更多相关文章
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- leetcode-algorithms-24 Swap Nodes in Pairs
leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
随机推荐
- java学习笔记 (8) —— Struts2 实现上传
1.新建upload.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...
- PHP扩展开发(2) - VS2013环境搭建
1. 安装VS2013 2. Cygwin安装 3. 下载Windows的PHP源码 4. 修改~/ext/ext_skel_win32.php 加上 $cygwin_path = 'c:\c ...
- ECSTORE关于后端FILTER条件的表现形式以及含义。
cstore关于后端filter条件的表现形式以及含义如下: $FILTERARRAY= ARRAY( 'THAN'=>' > '.$VAR, 'LTHAN'=>' < '.$ ...
- List容器
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...
- UITableView常用属性和方法 - 永不退缩的小白菜
UITableView常用属性和方法 - 永不退缩的小白菜 时间 2014-05-27 01:21:00 博客园精华区原文 http://www.cnblogs.com/zhaofucheng11 ...
- NSNumber与NSInteger的区别 -bei
基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值. 类的初始化,需用类自身的方法 (类方法). 代码中所创建的对 ...
- Ubuntu 怎么在右键添加打开终端
方法一: 搜索nautilus-open-terminal安装 命令行:sudo apt-get install nautilus-open-terminal (如果提示为找的什么的就s ...
- 单元测试之C/C++
如今TDD很火,我公司小,一般写代码不写测试用例的,一般就是随便测试下函数的输入输出,没用工具或框架来测试,非常简单,一点也不正规化. 在一种传统的结构化编程语言中,比如C,要进行测试的单元一般是函数 ...
- VS2010 Command Prompt Error:Cannot determine the location of the VS Common Tools folder
就在VS2010 Command Prompt 用vcvarsall.bat x64重新设置环境变量的时候,出现了标题中的错误.原因就在参考链接中 References: http://stackov ...
- hdu4623:crime 数学优化dp
鞍山热身赛的题,也是去年多校原题 题目大意: 求n个数的排列中满足相邻两个数互质的排列的数量并取模 当时的思路就是状压dp.. dp[i][state] state用二进制记录某个数是否被取走,i ...