merge two sorted linked list with duplicates
来源:互联网 发布:linux登录mysql 编辑:程序博客网 时间:2024/05/21 17:14
Question:
Given two sorted linked list, and merge them without using extra space (using constant space is allowed). For example, if 1 -> 2 -> 5 merges with 2 -> 4 -> 5, we have 1 -> 2 -> 4 -> 5
Analyze:
using four pointers:
h1: point to the head of list 1
h2: point to the head of list 2;
small: the smaller head between h1 and h2;
prev: the last node in the "new" list;
public static Node mergeSortedListWithDuplicates(Node h1, Node h2) {//one or both of the list(s) is (are) nullif(h1 == null) return h2;if(h2 == null ) return h1;Node newHead = (h1.data <= h2.data) ? h1 : h2;Node small = null;Node prev = newHead;//proceed to compare two heads and until one of them is null while (h1 != null && h2 != null) {if (h1.data <= h2.data) {small = h1;h1 = h1.next;} else {small = h2;h2 = h2.next;}prev.next = small;prev = small;}//deal with the remaining part of h1 or h2if (h1 == null) prev.next = h2;if (h2 == null) prev.next = h1;return newHead;}http:blog.csdn.net/beiyeqingteng
- merge two sorted linked list with duplicates
- merge two sorted linked list with duplicates
- merge two sorted linked list without duplicates
- Merge two sorted linked list
- Remove Duplicates from Sorted List && Merge Two Sorted Lists && Linked List Cycle && Remove Nth Node
- 21. Merge Two Sorted Lists(Linked List)
- 【LeetCode】【Linked List】Merge Two Sorted Lists
- Merge Two Linked List
- 21. Merge Two Sorted Lists(Linked List-Easy)
- Merge Two Sorted List
- merge two sorted list
- Merge Two Sorted List
- Merge Two Sorted List
- Merge Two sorted list
- Merge two sorted linked lists
- Merge two sorted linked lists
- Merge two sorted linked lists
- Merge two Sorted linked lists
- eliminate the duplicates in a sorted linked list (1)
- Manifolds with singularities
- SAP HANA 评估测试(SAP HANA Exam Test)
- Android中集成第三方软件包(.jar, .so)
- 使用nginx后如何在web应用中获取用户ip及原理解释
- merge two sorted linked list with duplicates
- 课程设计
- Android 关于android:name属性问题
- add two linked list as integer
- 【基础C&C++】内联函数&宏定义
- 用脚本建库ASM+单节点(增加节点,横向扩展RAC节点)步骤
- cscope+Vim的简单配置
- 使用NSTimer与iphone的简单动画,实现飘雪效果
- div