Prove safety offer56: Remove duplicate node list, ordered list, delete the duplicate node is not retained, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 1-> 2-> 5

1 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

2 ideas and methods

  (1) list is empty, not saying much , ;return NULL

  (2) If it is the first node and the head node a repeated, this situation can happen, the other end node should be removed, alternatively new node as the first node. How to deal with this particular case, apply for a multi-pointer on it.

3 C ++ core code

 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         if(NULL == pHead) return NULL; 
15         ListNode* pre = NULL, *cur = pHead;
16         while(NULL !=CUR) {
 17              IF (NULL! = cur> && cur the Next> Val cur ==> next-> Val) {     // involving dereference all, we must sentenced empty, it is professionalism! ! ! 
18 is                  int repeat = cur> Val;
 . 19                  ListNode * pNext;
 20 is                  the while (! = NULL && cur CUR> Val == repeat) {     // delete all values by repeat loop node 
21 is                      pNext = cur> Next ;
 22 is                      Delete CUR;
 23 is                      CUR = pNext;
 24                  }
 25              } the else {
 26 is                  pre = CUR;     //Unique pre node point 
27                  CUR = cur> Next;
 28              }
 29              
30              IF (== NULL pre) {
 31 is                  PHEAD = CUR;
 32              } the else {
 33 is                  pre-> Next = CUR;
 34 is              }
 35          }
 36          return PHEAD;
 37 [      }
 38 is };
View Code

Reference material

https://blog.csdn.net/qq_41822235/article/details/82832898

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11429134.html