To prove safety offer face questions delete duplicate node list

Title Description

In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5
 
Written 1:
Cur currently traversed node, if the value of cur-> next and cur-> next-> next is the same as described to find a repeating node a pointer to a new NEX, has been looking back, until the value is not equal to the value before repeating node val . The cur and nex to link to.
Such as 1,2,2,2,3
cur traverse to 1, then two nodes are cur after 2, then all the way back to find nex 3, 1 and 3 will be connected together, so that you have removed all the nodes value of 2.
 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6         val(x), next(NULL) {
 7     }
 8 };
 9 */
10 class Solution {
11 public:
12     ListNode* deleteDuplication(ListNode* pHead)
13     {
14         auto dummy=new ListNode(INT_MIN);
15         dummy->next=pHead;
16         auto cur=dummy;
17         while(cur->next and cur->next->next){
18             if(cur->next->val==cur->next->next->val){
19                 auto nex=cur->next;
20                 int temp=cur->next->val;
21                 while(nex and nex->val==temp){
22                     nex=nex->next;
23                 }
24                 cur->next=nex;
25             }
26             else{
27                 cur=cur->next;
28             }
29         }
30         return dummy->next;
31     }
32 };

2 writing:

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6         val(x), next(NULL) {
 7     }
 8 };
 9 */
10 class Solution {
11 public:
12     ListNode* deleteDuplication(ListNode* pHead)
13     {
14         auto dummy=new ListNode(INT_MIN);
15         dummy->next=pHead;
16         auto final_unique=dummy,cur=pHead,pre=dummy;
 . 17          the while (CUR) {
 18 is              IF (cur> Next and cur> Val cur ==> next-> Val) {
 . 19                  the while (cur> Next and cur> Val cur ==> next- > Val) {
 20 is                      cur = cur> Next;
 21 is                  }
 22 is                  // case cur is the last repeating node 
23 is              }
 24              the else {
 25                  final_unique-> Next = cur;
 26 is                  final_unique = cur;
 27              }
 28              cur = cur > Next;
 29          }
30         final_unique->next=nullptr;
31         return dummy->next;
32     }
33 };

 

Guess you like

Origin www.cnblogs.com/FdWzy/p/12305076.html