leetcode解题方案--024--Swap Nodes in Pairs
来源:互联网 发布:淘宝开店模板 编辑:程序博客网 时间:2024/06/14 14:28
题目
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.
分析
两种方法
一个是循环到底
另外一个是递归,递归的方法很巧妙,反正我是没想到。
看样子递归的功底不够啊
public static ListNode swapPairs(ListNode head) { ListNode ans = new ListNode(0); ans.next = head; ListNode last = ans; while (head != null && head.next != null) { last.next = head.next; head.next = head.next.next; last.next.next = head; head = head.next; last = last.next.next; } return ans.next; } public static ListNode swapPairs1(ListNode head) { if(head == null || head.next == null){ return head; } ListNode next = head.next; head.next = swapPairs(next.next); next.next = head; return next; }
阅读全文
0 0
- leetcode解题方案--024--Swap Nodes in Pairs
- LeetCode解题报告--Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs 解题报告
- 【LeetCode】Swap Nodes in Pairs 解题报告
- [leetcode javascript解题]Swap Nodes in Pairs
- Swap Nodes in Pairs LeetCode 解题报告
- 【LeetCode】Swap Nodes in Pairs 解题报告
- LeetCode 024 Swap Nodes in Pairs
- 【LeetCode OJ 024】Swap Nodes in Pairs
- leetcode 024 swap nodes in pairs
- 024 Swap Nodes in Pairs [Leetcode]
- LeetCode 024 Swap Nodes in Pairs
- [LeetCode]024-Swap Nodes in Pairs
- LeetCode 024 Swap Nodes in Pairs
- LeetCode 024 Swap Nodes in Pairs
- LeetCode-024 Swap Nodes in Pairs
- [leetcode] 24. Swap Nodes in Pairs 解题报告
- LeetCode 24. Swap Nodes in Pairs 解题报告
- Redis keys 命令
- 红黑树(RB-tree)比AVL树的优势在哪?
- BZOJ 2744 浅谈异或二进制分析及二分图最大团
- array_diff 数组差集
- SSM整合的一些配置(基于Maven工程,逆向工程,Restful风格)
- leetcode解题方案--024--Swap Nodes in Pairs
- Struts2_012_Struts中文处理
- PageHelper5.0.0分页插件与mybatis的集成
- spring httpMessageConverter 和 @ResponseBody
- 页面布局 --- 两列均分布局
- 基于Bmob,环信easeUI的校园二手交易市场——用户管理(含头像),数据管理,文件管理(图片管理),聊天功能的开发
- 看你的linux编译系统是32位还是64位最简单的方法以及不同数据类型占用字节个数
- 一些基础的数论知识
- 【Linux C 多线程编程】互斥锁与条件变量