美文网首页
leetcode 83. 删除排序链表中的重复元素

leetcode 83. 删除排序链表中的重复元素

作者: Source_Chang | 来源:发表于2020-10-13 14:10 被阅读0次

    leetcode

    C++:

    struct ListNode {
    public:
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    };
    
    
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            
            ListNode *previousNode = NULL;
            ListNode *currentNode = head;
            while ( currentNode ) {
                
                if ( !previousNode ) {
                    
                    previousNode = currentNode;
                    currentNode = currentNode -> next;
                    
                } else if ( previousNode -> val != currentNode -> val ) {
                    
                    previousNode = currentNode;
                    currentNode = currentNode -> next;
                    
                } else if ( previousNode -> val == currentNode -> val ) {
                    
                    ListNode *nextNode = currentNode -> next;
                    delete currentNode;
                    currentNode = nextNode;
                    previousNode -> next = currentNode;
                }
            }
    
            return head;
        }
    };
    

    相关文章

      网友评论

          本文标题:leetcode 83. 删除排序链表中的重复元素

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