美文网首页
83. Remove Duplicates from Sorte

83. Remove Duplicates from Sorte

作者: becauseyou_90cd | 来源:发表于2018-07-31 22:46 被阅读0次

https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

解题思路:

  1. 判断前后两个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;
}

}

相关文章

网友评论

      本文标题:83. Remove Duplicates from Sorte

      本文链接:https://www.haomeiwen.com/subject/qhpkvftx.html