⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 removedup.txt

📁 It is an ebook about data structures,mainly linked list
💻 TXT
字号:
  
   
Write a C program to remove duplicates from a sorted linked list? 

Discuss it!          



As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. When adjacent nodes are the same, remove the second one. There's a tricky case where the node after the next node needs to be noted before the deletion. 


// Remove duplicates from a sorted list 
void RemoveDuplicates(struct node* head) 
{ 
  struct node* current = head; 
  if (current == NULL) return; // do nothing if the list is empty 

  // Compare current node with next node 
  while(current->next!=NULL) 
  { 
      if (current->data == current->next->data) 
      { 
         struct node* nextNext = current->next->next; 
         free(current->next); 
         current->next = nextNext; 
      } 
      else 
      { 
         current = current->next; // only advance if no deletion 
      } 
   } 
} 


 
 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -