https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
解题思路:
- 判断前后两个node的val是否相等如果相等删除第二个, 如果不相等,对tempnode下移一位
代码:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode tempNode = head;
while(tempNode != null && tempNode.next != null){
if(tempNode.val == tempNode.next.val)
tempNode.next = tempNode.next.next;
else
tempNode = tempNode.next;
}
return head;
}
}
网友评论