Leetcode--Swap Nodes in Pairs
最傻的方法:
ListNode *swapPairs(ListNode *head) { if (head == NULL) return NULL; ListNode *temp = ); ListNode *head_2 = temp; while (head != NULL && head->next != NULL) { temp = temp->next = new ListNode(head->next->val); temp = temp->next = new ListNode(head->val); head = head->next->next; } if (head != NULL) temp->next = head; return head_2->next; }
好一点的方法
ListNode* swapPairs(ListNode* head) { if(!head || !head->next) return head; ListNode * temp = head->next; head->next = swapPairs(temp->next); temp->next = head; return temp; }
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 exam ...
- [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 成对交换节点
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 ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
随机推荐
- c# winform 调用js
1. 在winform中加入一个 WebBrowser 控件: 2. webBrowser控件加载html文件,html文件中包含了js代码, 代码如下(必须在 InitializeComponent ...
- SSH框架整合项目(一)——搭建平台和引入依赖
前言:这个项目是我的第一个实验性项目,最初的立意是制作一个个性化的BBS.由于BBS能够综合大部分功能,因此作为练手的项目来说再好不过.从写第一行代码到完成测试版大概历时2周.中间遇到了不少以前在学习 ...
- Hibernate 基础配置及常用功能(三)
本章重点讲述Hibernate对象的三种状态以及如何配置二级缓存 有关Hibernate的三种状态如何相互转换网上都能查到,官方文档描述的也比较详细.这里主要是针对几个重点方法做代码演示. 一.状态转 ...
- Kerberos
一.Kerberos Concept Kerberos是一种网络认证协议,其设计目标是通过密钥系统为客户机/服务器应用程序提供强大的认证服务,为通信双方提供双向身份认证. Kerberos关键术语: ...
- href="javascript:function()" 和onclick的区别
href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...
- mysql 删除时候有外键提示问题解决
在直接调用delete 语句的时候,如果出现了外键错误提示的时候,可以考虑用下面的语句执行. 原理是去除外键提示,先用外键约束,再取消外键约束即可 SET FOREIGN_KEY_CHECKS=1;D ...
- angular中不同controller传值问题
利用angularJS中service单例模式的特性,服务(service)提供了一种能在应用的整个生命周期内保持数据的方式,能够在控制器之间进行通信,且能保证数据的一致性. 一般我们都会封装serv ...
- Milliard Vasya's Function-Ural1353动态规划
Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning mathematician. He decided to make ...
- 函数对象(仿函数 functor)
简单地说,函数对象就是一个重载了()运算符的类实例,它可以像一个函数一样使用. #include <iostream> using namespace std; class Add { p ...
- web api authentication
最近在学习web api authentication,以Jwt为例, 可以这样理解,token是身份证,用户名和密码是户口本, 身份证是有有效期的(jwt 有过期时间),且携带方便(自己带有所有信息 ...