class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode **runner = &head;
if(!head  !head>next)return head;
while(*runner)
{
if((*runner)>next && (*runner)>next>val == (*runner)>val)
{
ListNode *temp = *runner;
while(temp && (*runner)>val == temp>val)
temp = temp>next;
*runner = temp;
}
else
runner = &((*runner)>next);
}
return head;
}
};
Is this the best C++ solution?


If using a fake head, like this. The idea is the same, if a duplicate is detected,firstly move to the next different number, and link it to its prefix.
class Solution { public: ListNode *deleteDuplicates(ListNode *head) { ListNode fakeHead(0); fakeHead.next = head; ListNode* pre = &fakeHead; ListNode* p = pre>next; while (p ) { ListNode* pn = p>next; if (pn && p>val == pn>val) { // move pn to next different value while (pn && p>val == pn>val) { pn = pn>next; } p = pn; pre>next = p; } else { pre = p; p = p>next; } } return fakeHead.next; } };

in C ?
struct ListNode* deleteDuplicates(struct ListNode* head) { struct ListNode** pNode = &head; if (!head  !head>next) return head; while (*pNode) { if ((*pNode)>next && (*pNode)>val==(*pNode)>next>val) { struct ListNode* node = *pNode; int val = node>val; while (node && node>val==val) { struct ListNode* tmp = node; node = node>next; free(tmp); } *pNode = node; } else { pNode = &((*pNode)>next); } } return head; }

It can be simplified!
ListNode* deleteDuplicates(ListNode* head) { ListNode **runner = &head; while (*runner) { if ((*runner)>next && (*runner)>next>val == (*runner)>val) { while ((*runner)>next && (*runner)>next>val == (*runner)>val) (*runner)>next = (*runner)>next>next; (*runner) = (*runner)>next; } else runner = &((*runner)>next); } return head; }

Some people does't like dummy node.
Personally I like it because it could improve readability.
Here is my sample code with dummy node using the same methodology.ListNode* deleteDuplicates(ListNode* head) { if(!head  !head>next) return head; ListNode dummy(head>val+1),*pre(&dummy),*cur(head); dummy.next = head; while(cur){ if(cur>next && cur>val == cur>next>val){ int cur_val = cur>val; while(cur && cur>val == cur_val) cur = cur>next; pre>next = cur; } else { pre = cur; cur = cur>next; } } return dummy.next; }

I think my code is clear.
ListNode* deleteDuplicates(ListNode* head) { if(head == nullptr) return nullptr; ListNode **p1 = &head; ListNode **p2 = &head; while(*p2 != nullptr) { if((*p2)>val != (*p1)>val) { if((*p1)>next != *p2) *p1 = *p2; else p1 = p2; } p2 = &(*p2)>next; } if((*p1)>next != *p2) *p1 = *p2; return head; }

what about mine:
bool isPowerOfFour(int num) { int mask = 0x55555555; // if you do 4^0 + 4^1 + 4^2 + 4^3 +..... in calc.exe in hex form // you will find this magical principle. // every time when add, the result is in form like "010101010101" // So, 0x55555555 can be the mask to see // whether the num is partly matching power of 4 if (num == 0) // Special case. return false; if (num & (num  1)) // Every num which is power of 4 just has one 1bit in its binary form return false; // num & (num  1) can judge if it is only one 1bit in its Binary if (num & (~mask)) // nums which can fit "only one 1 bit in its Binary" and "the 1 Bit return false; // is at correct position" are the power of 4. else return true; }

Same as yours Imaging!!! isn't it? But mine is more readable.
https://leetcode.com/discuss/109725/simplesolutionbecausefailedinterviewusinglikelymethon

I got one question. I got 8ms running time when I ran @tommy1122337 's solution. However, I got 12ms by simply replacing the code
if((*runner)>next && (*runner)>next>val == (*runner)>val)
by
if((*runner)>next && (*runner)>val == (*runner)>next>val).Can anyone tell me why this happen? Thanks.

@wen_chieh Sometimes the underlying
optimiser
works better in certain circumstances evenspace
orline feed
can have an influence at times. Hope someone can provide more details on this, funny question indeed.

@tommy1122337 and everyone,
I am just wondering, is it the use of
ListNode **runner
(double pointer) nesscarry?
can i simply use single pointer?

use one double pointer, no temp pointer, though the triple while loop looks weird
class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode **p = &head; while(*p) { while( (*p) && (*p)>next && (*p)>val == (*p)>next>val) { int val = (*p)>val; while((*p) && (*p)>val == val) { (*p) = (*p)>next; } } if(*p) { //if (*p) could be null, must be careful. p = &((*p)>next); } } return head; } };