写一个通用的算法,可以删除链表中值相同的结点的代码
时间: 2024-11-03 08:14:30 浏览: 37
为了删除链表中值相同的节点,你可以编写一个通用的递归算法,它会遍历链表并删除找到的第一个重复值。这里是一个简单的Python示例,使用双指针法:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def delete_duplicates(head):
if not head or not head.next:
return head
# 使用快慢指针
slow = fast = head
while fast and fast.next:
if fast.val == slow.val:
# 删除当前节点
slow.next = slow.next.next
else:
slow = slow.next
fast = fast.next.next
return head
```
这个算法的工作原理是,快指针每次移动两步,慢指针每次移动一步。如果发现两个节点值相等,那么快指针就直接跳过下一个,直到找到第一个不重复的节点,然后将慢指针指向的那个节点的next指向前移一位,跳过重复节点。
相关问题
写出一个通用的算法,删除链表中值的相同的节点烦人代码
创建一个通用算法来删除链表中值相同的节点通常涉及到迭代或递归,这取决于链表的具体情况。下面是一个基于迭代的通用算法示例,它适用于单向链表:
```python
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def delete_duplicates(head):
if head is None:
return head
current = head
prev = None # 用于记录前一个节点
while current is not None:
next_temp = current.next # 预备保存下一个节点
# 检查当前节点是否等于下一个节点
if current.value == next_temp.value:
# 如果当前节点是头节点,直接删除
if prev is None:
head = next_temp
else:
prev.next = next_temp
else:
# 否则,更新prev和current指针
prev = current
current = next_temp
return head
```
这个函数首先初始化`prev`为空,因为需要处理头节点可能是重复的情况。然后在循环中,每次比较当前节点和其后一个节点的值,如果它们相等,则更新`prev.next`指向`next_temp`之后的节点;如果不相等,`prev`和`current`都向前移动。
包知一个不带头结点的单链表head,结点结构为 Node。写出一个通用的算法,删除链 表中值相同的结点(如果有值相同的结点只保留首次出现的结点)。c语言
在C语言中,我们可以创建一个函数来遍历单链表并删除值相同的节点,仅保留第一次出现的节点。这是一个通用的算法:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建新节点
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed.\n");
return NULL;
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// 删除链表中值相同的节点
void deleteDuplicates(Node* head) {
if (head == NULL || head->next == NULL) { // 链表为空或只有一个元素的情况
return;
}
Node* current = head; // 当前节点
while (current != NULL && current->next != NULL) {
Node* temp = current->next; // 暂存当前节点的下一个
// 如果找到值相同的节点
while (temp != NULL && temp->data == current->data) {
// 将其从链表中移除
current->next = temp->next;
free(temp); // 释放内存
temp = current->next;
}
// 移动到下一个未检查的节点
current = current->next;
}
}
// 打印链表
void printList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
// 初始化链表示例
Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(4);
printf("Original list with duplicates:\n");
printList(head);
deleteDuplicates(head);
printf("\nList after deleting duplicates:\n");
printList(head);
return 0;
}
```
这个算法首先检查链表是否为空,然后逐个遍历节点。如果遇到值相同的节点,就将其从链表中删除,并将指针移动到下一个节点继续检查。最后,打印出处理后的链表。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)