Go语言程序从排序链表中删除重复值节点
在这篇 Go 语言文章中,我们将使用递归和迭代方法从排序链表中删除重复值节点。
链表是一种数据结构,由节点集合组成,每个节点包含一个值和指向列表中下一个节点的指针。
语法
func deleteDuplicates(head *Node) *Node{…}
deleteDuplicates() 函数用于从排序链表中删除重复值节点。它以指向头节点的指针作为参数。
算法
步骤 1 − 首先,我们需要导入 fmt 包。
步骤 2 − 现在,创建一个名为 Node 的链表单个节点的结构体。它包含两个成员,一个用于保存节点的数据值,第二个是指针,用于指向列表中的下一个节点。
步骤 3 − 定义函数 insert(),从头节点开始将节点插入链表中。
步骤 4 − 现在,创建一个名为 deleteDuplicates 的函数,该函数以链表的头作为输入,并返回更新后的链表的头。它使用迭代方法遍历列表并删除重复节点。
步骤 5 − 初始化当前指针以指向列表的头。并且使用循环遍历列表,直到当前指针变为 nil。
步骤 6 − 如果当前节点的数据值等于其下一个节点,则通过将下一个指针分配给下一个节点的下一个节点来删除下一个节点。
步骤 7 − 如果当前节点的数据值不等于其下一个节点,则通过将当前指针更新到其下一个字段来移动到下一个节点。
步骤 8 − 返回更新后的列表的头。
步骤 9 − 开始 main() 函数。在 main() 函数内部,调用 insert() 函数并将节点添加到链表中。
步骤 10 − 现在,调用 deleteDuplicates() 函数删除重复项并打印更新后的列表。
步骤 11 − 此外,使用 fmt.Println() 函数在屏幕上打印没有重复项的最终更新后的链表。
示例 1
在这个例子中,我们将定义一个使用迭代方法的 deleteDuplicates() 函数,该函数用于从排序链表中删除重复值节点。
package main
import "fmt"
type Node struct {
value int
next *Node
}
func insert(head **Node, value int) {
newNode := &Node{value: value}
if *head == nil || (*head).value >= value {
newNode.next = *head
*head = newNode
} else {
current := *head
for current.next != nil && current.next.value < value {
current = current.next
}
newNode.next = current.next
current.next = newNode
}
}
func deleteDuplicates(head *Node) *Node {
if head == nil {
return head
}
current := head
for current.next != nil {
if current.value == current.next.value {
current.next = current.next.next
} else {
current = current.next
}
}
return head
}
func printList(head *Node) {
for head != nil {
fmt.Printf("%d ->", head.value)
head = head.next
}
fmt.Println("nil")
}
func main() {
var head *Node
insert(&head, 4)
insert(&head, 3)
insert(&head, 1)
insert(&head, 2)
insert(&head, 3)
insert(&head, 4)
fmt.Println("Sorted Linked List:")
printList(head)
deleteDuplicates(head)
fmt.Println("List after deleting duplicate Value Nodes:")
printList(head)
}
输出
Sorted Linked List: 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> nil List after deleting duplicate Value Nodes: 1 -> 2 -> 3 -> 4 -> nil
示例 2
在这个例子中,我们将定义一个使用递归方法的 deleteDuplicates() 函数,该函数用于从排序链表中删除重复值节点。
package main
import (
"fmt"
)
type Node struct {
data int
next *Node
}
func deleteDuplicates(head *Node) *Node {
if head == nil || head.next == nil {
return head
}
head.next = deleteDuplicates(head.next)
if head.data == head.next.data {
return head.next
}
return head
}
func insert(head **Node, data int) {
newNode := &Node{data: data, next: *head}
*head = newNode
}
func printList(head *Node) {
for head != nil {
fmt.Printf("%d ->", head.data)
head = head.next
}
fmt.Println("nil")
}
func main() {
var head *Node = nil
insert(&head, 6)
insert(&head, 6)
insert(&head, 4)
insert(&head, 3)
insert(&head, 2)
insert(&head, 2)
insert(&head, 1)
fmt.Println("Sorted Linked List:")
printList(head)
head = deleteDuplicates(head)
fmt.Println("Linked List after deleting duplicate Value Nodes:")
printList(head)
}
输出
Sorted Linked List: 1 -> 2 -> 2 -> 3 -> 4 -> 6 -> 6 -> nil Linked List after deleting duplicates: 1 -> 2 -> 3 -> 4 -> 6 -> nil
结论
我们已经成功编译并执行了一个 Go 语言程序,该程序使用递归和迭代方法从排序链表中删除重复值节点,并附带两个示例。在第一个示例中,我们使用了迭代方法,在第二个示例中,我们使用了递归方法。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP