/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/*
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// write your code here
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
// int val = l1.val < l2.val ? l1.val : l2.val;
ListNode head = new ListNode(-1);
ListNode dummy1 = l1;
ListNode dummy2 = l2;
ListNode dummy3 = head;
// dummy != null 不是dummy.next != null
while (dummy1 != null && dummy2 != null){
if(dummy1.val < dummy2.val){
dummy3.next = new ListNode(dummy1.val);
dummy1 = dummy1.next;
} else {
dummy3.next = new ListNode(dummy2.val);
dummy2 = dummy2.next;
}
dummy3 = dummy3.next;
}
// 如果是dummy.next == null 那就不会进入这个判断了
if (dummy1 == null) {
dummy3.next = dummy2;
} else {
dummy3.next = dummy1;
}
return head.next;
}
}
Subscribe to:
Post Comments (Atom)
4. Ugly Number
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
-
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
-
Question: Given string A representative a positive integer which has N digits, remove any k digits of the number, the remaining digit...
-
Thinking: binary Tree 和 Stringbuilder 的联合题 对Stringbuilder 和StringTokenizer的使用方法的考验 public String serialize(TreeNode root) { ...
No comments:
Post a Comment