LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值。
思路:迭代法和递归法都容易写,就写个递归的了。
4ms
/**
* 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 || !head->next ) return head; ListNode *t=head->next;
head->next=swapPairs(t->next);
return t->next=head, t;
} };
AC代码
LeetCode Swap Nodes in Pairs 交换结点对(单链表)的更多相关文章
- [LeetCode]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 解题报告
		Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ... 
- [Leetcode] Swap nodes in pairs 成对交换结点
		Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ... 
- 024 Swap Nodes in Pairs 交换相邻结点
		给定一个链表,对每两个相邻的结点作交换并返回头节点.例如:给定 1->2->3->4,你应该返回 2->1->4->3.你的算法应该只使用额外的常数空间.不要修改列 ... 
- [LeetCode]Swap Nodes in Pairs 成对交换
		Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ... 
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
		Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ... 
- [leetcode]24. Swap Nodes in Pairs交换链表的节点
		感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ... 
- [LeetCode] Swap Nodes in Pairs 成对交换节点
		Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ... 
- leetcode—Swap Nodes in Pairs
		1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ... 
随机推荐
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads
			题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个 ... 
- Spring中HibernateCallback的用法(转)
			Hibernate的复杂用法HibernateCallback HibernateTemplate还提供一种更加灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式.Hiber ... 
- 【BestCoder】【Round#29】
			T1 啊……a^b 与 c^d比较大小,我们可以两边取对数,转化成 log(a^b)=b*log(a) 和d*log(c) 这样就能直接算了……然后稍微搞一下精度什么的就A了=.= //BC #29 ... 
- 17.2 The DispatcherServlet
			综述: Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed arou ... 
- WebStorm 9 注册码
			UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA INauvJkeVJB ... 
- Follow Path -》 Unity3d通用脚本
			PathDefinition.cs using UnityEngine; using System.Collections; using System.Collections.Generic; usi ... 
- 如何使用yum来下载RPM包而不进行安装
			如何使用yum来下载RPM包而不进行安装 2015-03-23 13:15 theo-l译 linux.cn 字号:T | T yum是基于Red Hat的系统(如CentOS.Fedora.RHEl ... 
- HTTP/2 对 Web 性能的影响(上)
			一.前言 HTTP/2 于 2015 年 5 月正式推出.自诞生以来,它就一直在影响着网络性能最佳实践.在本篇文章中,我们将讨论 HTTP/2 的二进制帧.延迟削减.潜在利弊以及相应的应对措施. 超文 ... 
- C++ char*,const char*,string,int 的相互转换
			C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ... 
- POJ2488A Knight's Journey
			http://poj.org/problem?id=2488 题意 : 给你棋盘大小,判断马能否走完棋盘上所有格子,前提是不走已经走过的格子,然后输出时按照字典序排序的第一种路径 思路 : 这个题吧, ... 
