http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
链表的去重,要考虑链表的本质。
#include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if(!head) return head; ListNode *currNode = head->next; ListNode *lastInList = head; //记录新列表中最后一个节点 while(currNode) { if(currNode->val == lastInList->val) { currNode = currNode->next; if(currNode==NULL) lastInList->next = NULL; continue; } else { lastInList->next = currNode; lastInList = currNode; currNode = currNode->next; } } return head; } }; int main() { Solution myS; ListNode *n1 = new ListNode(1); ListNode *n2 = new ListNode(1); ListNode *n3 = new ListNode(2); ListNode *n4 = new ListNode(3); ListNode *n5 = new ListNode(3); n1->next = n2; n2->next = n3; n3->next = n4; n4->next = n5; myS.deleteDuplicates(n1); return 0; }