原题
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
1->1->2,
return 1->2.
Given
1->1->2->3->3,
return 1->2->3.
题目分析
删除单链表中的所有重复节点。 指针diff 指向链表中的不同值,tmp指向每一个节点,内层while循环不断刷新tmp,跳出循环后表示找到一个新的不同节点tmp.next,赋值给diff.next,表示找到一个新的不同值。
代码实现
public
ListNode DeleteDuplicates(
ListNode head)
{
ListNode diff = head;
ListNode tmp = head;
while (tmp != null && tmp.
next != null)
{
while (tmp.
next != null && tmp.
next.val == tmp.val)
{
tmp = tmp.
next;
}
diff.
next = tmp.
next;
//找到一个新值
diff = diff.
next;
//迭代
tmp = diff;
}
return head;
}
LinkedList 更多题目
Easy部分标签为LinkedList 的所有题目
转载请注明原文地址: https://ju.6miu.com/read-16034.html