Algorithms—24.Swap Nodes in Pairs
来源:互联网 发布:爱知时计科技有限公司 编辑:程序博客网 时间:2024/06/14 07:16
思路:递归,然后交换相邻的两个值。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode swapPairs(ListNode head) { if (head==null||head.next==null) {return head;}else {int val=head.val;head.val=head.next.val;head.next.val=val;head.next.next=swapPairs(head.next.next);} return head; }}
耗时:300ms,中下游
0 0
- Algorithms—24.Swap Nodes in Pairs
- LeetCode Algorithms #24 <Swap Nodes in Pairs>
- LeetCode Algorithms #24 <Swap Nodes in Pairs>
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 依赖注入——让iOS代码更简洁
- 数字字符、数组处理
- hdu5303 Delicious Apples
- Oracle存在修改,不存在插入记录
- 感知机(perceptron)学习算法的原始形式
- Algorithms—24.Swap Nodes in Pairs
- Linux 文件系统的目录结构
- String,StringBuilder和StringBuffer区别及使用场景
- KMP算法(部分重要代码)
- caffe for windows 训练cifar10
- 1002 A + B Problem II
- linux查看硬件信息及驱动设备相关整理
- iOS Sprite Kit教程之使用帮助文档以及调试程序
- 基于Python的标准库tkinter创建面向对象的GUI应用程序