6.2 Merge Two Sorted Lists
来源:互联网 发布:公知和五毛是什么意思 编辑:程序博客网 时间:2024/06/08 04:48
原题链接:https://oj.leetcode.com/problems/merge-two-sorted-lists/
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.
我的思路:可以再create一个新的链表,作为返回结果吗?可以。如果不行,该怎么做?
Time: O(min(m, n)), Space: O(1)
public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode prev = new ListNode(0);//the result list prev.next = null; ListNode temp = prev; while(l1!=null && l2!=null){ if(l1.val < l2.val){ prev.next = l1; l1 = l1.next; } else{ prev.next = l2; l2 = l2.next; } prev = prev.next; } if(l1!=null) prev.next = l1;//当两链表之一还有剩余时,只需把当前prev.next指向头节点,不用再逐个遍历剩下节点。因此时间复杂度是O(min(m,n)) else prev.next = l2; return temp.next; }}
@8.30.2014 已做第二遍。不用再做。
0 0
- 6.2 Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- LeetCode刷题笔录Search in Rotated Sorted Array
- HDOJ 1013 Digital Roots
- linux设备驱动程序_hello word 模块编译各种问题集锦
- 6.1 Merge Sorted Array
- 游戏服务器之网络收发线程处理详细分析
- 6.2 Merge Two Sorted Lists
- 生成规格大小的图片,质量压缩
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次
- HER引发的想法---微软小冰
- 0x000000068.0x000000097.0x000000002.67
- 104.0x0097.0x0002.68
- Socket 长连接 心跳包
- MySQL CMake参数说明手册
- html实现页面内加横向滚动条