Guest User

Untitled

a guest
Jun 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. class Solution {
  2. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  3. ListNode dummy = new ListNode(0), p1 = l1, p2 = l2, p = dummy;//Use a dummy node so that we don't need to check p1.val and p2.val
  4. while(p1 != null && p2 != null) {
  5. if(p1.val < p2.val) {
  6. p.next = p1;
  7. p1 = p1.next;
  8. } else {
  9. p.next = p2;
  10. p2 = p2.next;
  11. }
  12. p = p.next;
  13. }
  14. if(p1 == null)
  15. p.next = p2;
  16. else
  17. p.next = p1;
  18. return dummy.next;
  19. }
  20. }
  21. /**
  22. * Definition for singly-linked list.
  23. * public class ListNode {
  24. * int val;
  25. * ListNode next;
  26. * ListNode(int x) { val = x; }
  27. * }
  28. */
Add Comment
Please, Sign In to add comment