[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/feixiangcode/algorithm/master/Week_01/id_126/LeetCode_21_126.java [Back]  [Original]

/**
 * 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
        //listnodeListnode
        ListNode listNode = new ListNode(0);
        ListNode current = listNode;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                //l1listnode  l1
                current.next = l1;
                current = l1;
                l1 = l1.next;

            } else {
                //l2listnode  l2
                current.next = l2;
                current = l2;
                l2 = l2.next;

            }

        }

        if (l1 == null) {
            current.next = l2;
        }
        if (l2 == null) {
            current.next = l1;
        }

        return listNode.next;
    }
}

Web Proxy Viewer  |  New URL  |  Original Page