链表问题---合并两个有序的单链表
2017-11-21 14:07
232 查看
【题目】
给定两个有序单链表的头节点head1和head2,请合并两个有序链表,合并后的链表依然有序,并返回合并后链表的头节点。
【代码实现】
给定两个有序单链表的头节点head1和head2,请合并两个有序链表,合并后的链表依然有序,并返回合并后链表的头节点。
【代码实现】
#python3.5 def merge(head1, head2): if head1 == None or head2 == None: return head1 if head2 == None else head2 head = head1 if head1.val < head2.val else head2 cur1 = head1 if head == head1 else head2 cur2 = head1 if head == head2 else head2 pre = None while cur1 != None and cur2 != None: if cur1.val <= cur2.val: pre = cur1 cur1 = cur1.next else: next = cur2.next pre.next = cur2 cur2.next = cur1 pre = cur2 cur2 = next pre.next = cur1 if cur2 == None else cur2 return head
相关文章推荐
- 面试中常见链表问题1:合并两个有序链表
- 两个有序链表的合并问题
- 数据结构_中国大学MOOC(慕课)——两个有序链表序列的合并问题
- 02-线性结构1 两个有序链表序列的合并 (15分)
- 将两个有序链表合并成一个有序链表
- 5-51 两个有序链表序列的合并 (20分)
- 合并两个有序链表(非交叉)
- 2-11. 两个有序链表序列的合并(15)
- 链表(14)----合并两个有序链表
- 合并两个有序的链表和计算1+2+3+4....
- 9、单链表的合并,两个有序单链表,合并为一个有序链表。
- 合并两个有序链表
- Leetcode Merge Two Sorted Lists 合并两个有序链表
- 合并两个有序链表的算法
- c++ 将两个有序的单链表AB合并到新的单链表C中
- 5-51 两个有序链表序列的合并 (20分)
- 每天做死一道题——两个有序链表序列的合并
- LeetCode Merge Two Sorted Lists(合并两个有序链表)
- 合并两个有序的链表
- 剑指offer 面试题17 合并两个有序链表