常州营销网站建设,开化网站建设公司,互联网产品营销策划方案,网络工具c语言i和i程序Given a sorted linked list (elements are sorted in ascending order). Eliminate duplicates from the given LL, such that output LL contains only unique elements. 给定一个排序的链表(元素按升序排序)。 从给定的LL中消除重复项#xff0c;以便输出LL仅…c语言i和i程序Given a sorted linked list (elements are sorted in ascending order). Eliminate duplicates from the given LL, such that output LL contains only unique elements. 给定一个排序的链表(元素按升序排序)。 从给定的LL中消除重复项以便输出LL仅包含唯一元素。 Input format: Linked list elements (separated by space and terminated by 1) 输入格式链接列表元素(以空格分隔并以1终止) Sample Input 1 :
1 2 3 3 3 4 4 5 5 5 7 -1
Sample Output 1 :
1 2 3 4 5 7
Description: 描述 In this question, we are given a sorted linked list with duplicate elements in it. Our task is to remove the duplicate nodes of the linked list. Since the list is sorted, the duplicate elements will be present in consecutive orders, that makes the work a lot easier. 在这个问题中我们得到一个包含重复元素的排序链表。 我们的任务是删除链表的重复节点。 由于列表已排序因此重复的元素将以连续的顺序显示这使工作变得更加容易。 Example: 例 Lets the list be:
1-2-3-3-4-4-4-NULL
The modified list will be:
1-2-3-4-NULL
Solution Explanation: 解决方案说明 To solve this problem, we keep the temp pointer pointing to node. While(temp - next ! NULL), we check if the data of temp is equal to data of temp-next. If they are equal, we will point the temp- next to temp - next -next. Original LL: 3- 3- 4- ... After Change: 3- 4- ... Leaving out the middle 3- 为了解决这个问题我们使临时指针指向节点。 while(temp- next NULL)时我们检查temp的数据是否等于temp- next的数据。 如果它们相等则将temp-指向temp- next- next。 原始LL3- 3- 4- ... 更改后3- 4- ...省略中间3- This will keep on until we reached the end of the list and return head at the end. 这将一直持续到我们到达列表的末尾并返回末尾。 Steps 脚步 1. 1-2-3-3-4-4-4-NULL, temp 1
2. 1-2-3-3-4-4-4-NULL, temp 2
3. 1-2-3-3-4-4-4-NULL, temp 3
4. 1-2-3-4-4-4-NULL, temp 4
5. 1-2-3-4-4-NULL, temp 4
Finally,
1-2-3-4-NULL
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
Function: 功能 Node *removeDuplicate(Node* head){
//temp pointing to head
Node *temp head;
while(temp-next ! NULL temp ! NULL){
//Duplicate Found
if(temp-data temp-next-data){
//DUplicate Removed
temp - next temp -next -next;
}
else{
//No Duplicate Present
temp temp-next;
}
}
//Return Head
return head;
}
C Code: C 代码 #includebits/stdc.h
using namespace std;
struct Node{// linked list Node
int data;
Node * next;
};
Node *newNode(int k){ //defining new node
Node *temp (Node*)malloc(sizeof(Node));
temp-data k;
temp-next NULL;
return temp;
}
//Used to add new node at the end of the list
Node *addNode(Node* head, int k){
if(head NULL){
head newNode(k);
}
else{
Node * temp head;
Node * node newNode(k);
while(temp-next! NULL){
temp temp-next;
}
temp- next node;
}
return head;
}
// Used to create new linked list and return head
Node *createNewLL(){
int cont 1;
int data;
Node* head NULL;
while(cont){
coutEnter the data of the Nodeendl;
cindata;
head addNode(head,data);
coutDo you want to continue?(0/1)endl;
cincont;
}
return head;
}
//To print the Linked List
void *printLL(Node * head){
while(head! NULL){
couthead-data-;
head head- next;
}
coutNULLendl;
}
//Function
Node *removeDuplicate(Node* head){
//temp pointing to head
Node *temp head;
while(temp-next ! NULL temp ! NULL){
//Duplicate Found
if(temp-data temp-next-data){
//DUplicate Removed
temp - next temp -next -next;
}
else{
//No Duplicate Present
temp temp-next;
}
}
//Return Head
return head;
}
//Driver Main
int main(){
Node * head createNewLL();
coutThe linked list isendl;
printLL(head);
head removeDuplicate(head);
coutThe new Linked List is endl;
printLL(head);
return 0;
}
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
.minHeight{
min-height: 250px;
}
@media (min-width: 1025px){
.minHeight{
min-height: 90px;
}
}
Output 输出量 Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
3
Do you want to continue?(0/1)
1
Enter the data of the Node
4
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
0
The linked list is
1-1-2-2-3-4-5-5-5-NULL
The new Linked List is
1-2-3-4-5-NULL
翻译自: https://www.includehelp.com/cpp-programs/eliminate-duplicates-from-linked-list.aspxc语言i和i程序