LeetCode – 21. 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.

/** * Definition for singly-linked list. * public class ListNode { * int val; currs = res; while(curr1 !=null || curr2 != null){ if(curr1 == null){ currs.next = curr2; currs = currs.next; curr2 = curr2.next; continue; } if(curr2 == null){ currs.next = curr1; currs = currs.next; curr1 = curr1.next; continue; } if(curr1.val < curr2.val){ currs.next = curr1; currs = currs.next; curr1 = curr1.next; } else{ currs.next = curr2; currs = currs.next; curr2 = curr2.next; } } currs.next = null; return res.next; } }

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"LeetCode – 21. Merge Two Sorted Lists"

213
0 0 213

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top