Skip to content

Instantly share code, notes, and snippets.

@RakhmedovRS
Last active July 23, 2022 22:54
Show Gist options
  • Save RakhmedovRS/9d7bff26fae314a46d13420028842992 to your computer and use it in GitHub Desktop.
Save RakhmedovRS/9d7bff26fae314a46d13420028842992 to your computer and use it in GitHub Desktop.
Merge Two Sorted Lists
public ListNode mergeTwoLists(ListNode l1, ListNode l2)
{
ListNode dummy = new ListNode(0);
ListNode current = dummy;
while (l1 != null && l2 != null)
{
if (l1.val <= l2.val)
{
current.next = l1;
l1 = l1.next;
}
else
{
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
if (l1 != null)
{
current.next = l1;
}
if (l2 != null)
{
current.next = l2;
}
return dummy.next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment