用C#语言实现删除排序链表中的重复元素
题目详情:
解题思路:
双指针,一个指向不重复的最后一节点,一个负责遍历。要注意遍历结束后,将第一个指针的下一个节点设为null。
力扣解题:
代码实现:
public class Solution {
public ListNode DeleteDuplicates(ListNode head) {
if (head == null || head.next == null)
{
return head;
}
ListNode NowNode = head;
ListNode loopNode = head.next;
while (loopNode != null)
{
if (loopNode.val != NowNode.val)
{
NowNode.next = loopNode;
NowNode = loopNode;
}
loopNode = loopNode.next;
}
NowNode.next = null;
return head;
}
}