Leetcode 21(Java)
来源:互联网 发布:7.62*63步枪子弹数据 编辑:程序博客网 时间:2024/06/08 05:35
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
题目很简单有很简短,实际上对于链表的题目很容易想到递归的方法。新建一个头节点,不断拿两个链表的节点比较,谁小就链接上去。然后那个链表往下跳一个节点,AC码如下:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1==null)return l2; if(l2==null)return l1; ListNode resultHead; if(l1.val<l2.val){ resultHead=l1; resultHead.next=mergeTwoLists(l1.next,l2); }else{ resultHead=l2; resultHead.next=mergeTwoLists(l1,l2.next); } return resultHead; }
阅读全文
0 0
- (java)leetcode-21
- Leetcode 21(Java)
- Leetcode(java)
- leetcode java
- LeetCode 21:Merge Two Sorted Lists (Java)
- [LeetCode]Next Permutation java leetcode
- LeetCode: Two Sum (Java)
- [Leetcode] Two Sum (Java)
- [Leetcode] ZigZag Conversion (Java)
- [Leetcode] Reverse Integer (Java)
- [Leetcode] Palindrome Number (Java)
- [Leetcode] Valid Parentheses (Java)
- [Leetcode] Generate Parentheses (Java)
- [Leetcode] Remove Element (Java)
- [Leetcode] Implement strStr() (Java)
- [Leetcode] Next Permutation (Java)
- [Leetcode] Valid Sudoku (Java)
- [Leetcode] Sudoku Solver (Java)
- git学习笔记
- [P1026]统计单词个数
- Leetcode 114 Flatten Binary Tree to Linked List
- 第十四章:表单脚本(文本框脚本)
- JavaScript模仿微博发布效果
- Leetcode 21(Java)
- connect函数的探究
- Java8 Stream API HDU简单题集锦
- Android Studio生成成员变量时自动加上m前缀
- AI的博客资料等
- Jetty实战之 嵌入式Jetty运行Servlet
- python之多进程和进程池(multiprocessing库)
- 镜像与代理、证书与授权
- CSS居中