Q82. Remove Duplicates from Sorted List II
Last updated
Was this helpful?
Last updated
Was this helpful?
直达:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.
For example,
Given1->2->3->3->4->4->5
, return1->2->5
.
Given1->1->1->2->3
, return2->3
.
增加两个辅助指针,d_head作为伪头节点,p_haed辅助删除数据
需要注意的几个边界情况及解决方案
头节点需要删除:设置伪头节点,伪头节点的下个元素是head,返回的时候返回d_head的下一个节点
最后一个节点需要删除:判断是否是最后一个节点,如果是的话将p指向空,返回结果