参考了https://blog.csdn.net/geekmanong/article/details/51097196
先反转后面的链表,从最后面的两个结点开始反转,依次向前,将后一个链表结点指向前一个结点,注意每次反转后要将原链表中前一个结点的指针域置空,表示将原链表中前一个结点指向后一个结点的指向关系断开。
C++规定,一个指针变量加/减一个整数是将该指针变量的原值(是一个地址)和它指向的变量所占用的内存单元字节数相加或相减。这个不是排序好的,不好使用双指针?set和hashset的区别。基于红黑树实现,红黑树具有自动排序的功能,基于哈希表的无自动排序功能。
ListNode* removeDuplicateNodes(ListNode* head) { if(head==NULL||head->next==NULL) return head; ListNode *pos=head;//*cur=NULL;//pos是用来枚举的 set<int> s; s.insert(head->val); while(pos->next!=NULL){ ListNode* cur=pos->next; if(!s.count(cur->val)){ s.insert(cur->val); pos=pos->next; } else{ pos->next=pos->next->next;//这里没有释放内存 } } return head; }当需要删除节点的时候,最好枚举前驱节点。
ListNode* deleteNode(ListNode* head, int val) { ListNode *p=head;//p用来枚举 if(head==NULL) return NULL; if(head->val==val) return head->next; while(p->next!=NULL){ ListNode *cur=p->next; if(cur->val==val){ p->next=p->next->next; return head; } else{ p=p->next; } } return head; }