C# 代码实现,为什么返回的时候必须是dummyHead.next;
返回head不行???
dummyHead.next指向的不也是head吗?所以删除节点后,应该返回head也可以啊?
public ListNode RemoveElements(ListNode head, int val)
{
if (head == null)
return null;
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode cur = dummyHead;
while (cur.next != null)
{
if (cur.next.val == val)
cur.next = cur.next.next;
else
cur = cur.next;
}
return dummyHead.next;
}