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 語言程式,該程式使用遞迴和迭代方法從排序連結串列中刪除重複值節點,並附帶兩個示例。在第一個示例中,我們使用了迭代方法,在第二個示例中,我們使用了遞迴方法。

更新於: 2023年5月10日

280 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.