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. » Solve this problem [Thoughts] 简单的实现,也没什么可说的。 [Code] 1:ListN
2.Solutions: 1/**2* Created by sheepcore on 2019-05-103* Definition for singly-linked list.4* public class ListNode {5* int val;6* ListNode next;7* ListNode(int x) { val = x; }8* }9*/10classSolution {11publicListNode mergeTwoLists(ListNode l1, ListNode l2) {12ListNode head =n...
next return list class Solution: def mergeTwoLists(self, head1, head2): if not head1: # 如果head1为空,直接返回head2 return head2 if not head2: # 如果head2为空,直接返回head1 return head1 pre = ListNode(0) # 使用哑结点简化操作 head = pre while head1 and head2: if head1.val ...
{ // next: None, // val // } // } // } impl Solution { pub fn merge_two_lists(mut list1: Option<Box<ListNode>>, mut list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { // 使用一个哨兵结点 head_pre ,方便后续处理 let mut head_pre = ListNode::new(0); // 使用尾...
https://leetcode.com/problems/merge-two-sorted-lists/ 我的思路就是找到第一个j > i的,然后用pre_j串起来。然后相应地对i再做一次。这种思路不太好。 note在指针前进的时候,判断越界的语句要放在if 或者while的前半部分。 my code: class Solution(object): ...
class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(0), ptr = head; while (l1 != null && l2 != null) { if (l1.val > l2.val) { ptr.next = l2; ptr = l2; l2 = l2.next; ...
* 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; ...
public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // 创建一个dummy头,从后面开始接 ListNode dummy = new ListNode(0); ListNode curr = dummy; // 依次比较拼接 while(l1 != null && l2 != null){ if(l1.val <= l2.val){ ...
链接:https://leetcode.cn/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 可以看到,有一个链表是null的情况下,直接return了非空链表 ...
class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode head = new ListNode(0); ListNode cur = head; while (l1 != null && l2 != null) { ...