LeetCode_21. Merge Two Sorted Lists
2018-01-12 17:35
405 查看
题目
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.Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
答案1:先处理两个链表的第一个节点,得出结果的第一个结点
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head, p1 = l1, p2 = l2, p; if (p1 == null) return p2; if (p2 == null) return p1; if (p1.val < p2.val) { head = p1; p1 = p1.next; } else { head = p2; p2 = p2.next; } p = head; while (p1 != null && p2 != null) { if (p1.val < p2.val) { p.next = p1; p1 = p1.next; } else { p.next = p2; p2 = p2.next; } p = p.next; } if (p1 == null) p.next = p2; if (p2 == null) p.next = p1; return head; } }
答案二:为结果链表增加头结点,最后返回head.next
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(0), p1 = l1, p2 = l2, p = head; while (p1 != null && p2 != null) { if (p1.val < p2.val) { p.next = p1; p1 = p1.next; } else { p.next = p2; p2 = p2.next; } a155 p = p.next; } if (p1 == null) p.next = p2; if (p2 == null) p.next = p1; return head.next; } }
在做题的过程中遇到的问题主要是想移动前后两个指针来达到插入目的。但是这不是插入,是链表排序,只需要将待排的那个节点指针和最后的结果序列连接起来就行了,是指针操作不是真实移动。
答案三:递归
自己的错误答案:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(0), p1 = l1, p2 = l2, p = head; while (p1 != null && p2 != null) { if (p1.val < p2.val) { p.next = p1; p1 = mergeTwoLists(p1.next, p2); } else { p.next = p2; p2 = mergeTwoLists(p1, p2.next); } } if (p1 == null) { p.next = p2; } if (p2 == null) { p.next = p1; } return head.next; } }
问题在于没有理解递归的真正含义,递归就是将整个问题划分为子问题,代码中返回的结果应该就是最终的结果,而子问题的结果就是“下一次递归的最终结果”,交给子问题来完成。
正确的递归:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1==null) return l2; if(l2==null) return l1; if(l1.val<l2.val){ l1.next = mergeTwoLists(l1.next,l2); return l1; } else{ l2.next = mergeTwoLists(l1,l2.next); return l2; } } }
相关文章推荐
- leetcode 21. Merge Two Sorted Lists
- leetcode21. Merge Two Sorted Lists
- 21. Merge Two Sorted Lists-leetcode-java
- LeetCode 21. Merge Two Sorted Lists 两个有序列表合成一个
- LeetCode-21. Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists 题解 —— Java
- [LeetCode]21. Merge Two Sorted Lists
- LeetCode题解(Week2):21. Merge Two Sorted Lists
- 21. Merge Two Sorted Lists leetcode(lists)
- [LeetCode] 21. Merge Two Sorted Lists 解题思路
- LeetCode 21. Merge Two Sorted Lists
- LeetCode-Easy部分中标签为LinkedList 21. Merge Two Sorted Lists
- 21. Merge Two Sorted Lists leetcode Python 2016 new Season
- 2018.1.8 LeetCode 21. Merge Two Sorted Lists 【链表的基本运用】
- leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)
- LeetCode: 21. Merge Two Sorted Lists
- leetcode21. Merge Two Sorted Lists
- [leetcode] 21. Merge Two Sorted Lists 解题报告
- LeetCode21. Merge Two Sorted Lists