深谈新加坡不允许随地大小便的规定 (转载)# Joke - 肚皮舞运动
f*e
1 楼
刚刚学习链表, 请问 head = dummy 是把dummy的值赋给 head吗? 还是其他含义?
为什么最后返回dummy.next 就是返回合并之后的整个链表?貌似 循环过程没dummy 啥
事?
private ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(-1);
ListNode head = dummy;
while (head1 != null && head2 != null ) {
if (head1.val < head2.val) {
head.next = head1;
head1 = head1.next;
}else {
head.next = head2;
head2 = head2.next;
}
head = head.next;
}
if (head1 != null) {
head.next = head1;
}
else {
head.next = head2;
}
return dummy.next;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
为什么最后返回dummy.next 就是返回合并之后的整个链表?貌似 循环过程没dummy 啥
事?
private ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(-1);
ListNode head = dummy;
while (head1 != null && head2 != null ) {
if (head1.val < head2.val) {
head.next = head1;
head1 = head1.next;
}else {
head.next = head2;
head2 = head2.next;
}
head = head.next;
}
if (head1 != null) {
head.next = head1;
}
else {
head.next = head2;
}
return dummy.next;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}