diff --git a/Algorithms/82.Remove Duplicates from Sorted List II/README.md b/Algorithms/82.Remove Duplicates from Sorted List II/README.md new file mode 100644 index 00000000..654a7d2d --- /dev/null +++ b/Algorithms/82.Remove Duplicates from Sorted List II/README.md @@ -0,0 +1,23 @@ +# [82. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) + +## 题目 + +Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. + +Example 1: + +``` +Input: 1->2->3->3->4->4->5 +Output: 1->2->5 +``` + +Example 2: + +``` +Input: 1->1->1->2->3 +Output: 2->3 +``` + +## 题目大意 + +删除链表中重复的结点,只要是有重复过的结点,全部删除。 \ No newline at end of file diff --git a/Algorithms/82.Remove Duplicates from Sorted List II/Remove Duplicates from Sorted List II.go b/Algorithms/82.Remove Duplicates from Sorted List II/Remove Duplicates from Sorted List II.go new file mode 100644 index 00000000..d6a168b5 --- /dev/null +++ b/Algorithms/82.Remove Duplicates from Sorted List II/Remove Duplicates from Sorted List II.go @@ -0,0 +1,69 @@ +package leetcode + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func deleteDuplicates1(head *ListNode) *ListNode { + if head == nil { + return nil + } + if head.Next == nil { + return head + } + newHead := &ListNode{Next: head, Val: -999999} + cur := newHead + last := newHead + front := head + for front.Next != nil { + if front.Val == cur.Val { + // fmt.Printf("相同节点front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val) + front = front.Next + continue + } else { + if cur.Next != front { + // fmt.Printf("删除重复节点front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val) + last.Next = front + if front.Next != nil && front.Next.Val != front.Val { + last = front + } + cur = front + front = front.Next + } else { + // fmt.Printf("常规循环前front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val) + last = cur + cur = cur.Next + front = front.Next + // fmt.Printf("常规循环后front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val) + + } + } + } + if front.Val == cur.Val { + // fmt.Printf("相同节点front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val) + last.Next = nil + } else { + if cur.Next != front { + last.Next = front + } + } + return newHead.Next +} + +func deleteDuplicates2(head *ListNode) *ListNode { + if head == nil { + return nil + } + if head.Next != nil && head.Val == head.Next.Val { + for head.Next != nil && head.Val == head.Next.Val { + head = head.Next + } + return deleteDuplicates(head.Next) + } else { + head.Next = deleteDuplicates(head.Next) + } + return head +} diff --git a/Algorithms/82.Remove Duplicates from Sorted List II/Remove Duplicates from Sorted List II_test.go b/Algorithms/82.Remove Duplicates from Sorted List II/Remove Duplicates from Sorted List II_test.go new file mode 100644 index 00000000..ebda8a62 --- /dev/null +++ b/Algorithms/82.Remove Duplicates from Sorted List II/Remove Duplicates from Sorted List II_test.go @@ -0,0 +1,82 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question82 struct { + para82 + ans82 +} + +// para 是参数 +// one 代表第一个参数 +type para82 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans82 struct { + one []int +} + +func Test_Problem82(t *testing.T) { + + qs := []question82{ + + question82{ + para82{[]int{1, 1, 2, 2, 3, 4, 4, 4}}, + ans82{[]int{3}}, + }, + + question82{ + para82{[]int{1, 1, 1, 1, 1, 1}}, + ans82{[]int{}}, + }, + + question82{ + para82{[]int{1, 1, 1, 2, 3}}, + ans82{[]int{2, 3}}, + }, + + question82{ + para82{[]int{1}}, + ans82{[]int{1}}, + }, + + question82{ + para82{[]int{}}, + ans82{[]int{}}, + }, + + question82{ + para82{[]int{1, 2, 2, 2, 2}}, + ans82{[]int{1}}, + }, + + question82{ + para82{[]int{1, 1, 2, 3, 3, 4, 5, 5, 6}}, + ans82{[]int{2, 4, 6}}, + }, + + question82{ + para82{[]int{1, 1, 2, 3, 3, 4, 5, 6}}, + ans82{[]int{2, 4, 5, 6}}, + }, + + question82{ + para82{[]int{0, 1, 2, 2, 3, 4}}, + ans82{[]int{0, 1, 2, 2, 3, 4}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 82------------------------\n") + + for _, q := range qs { + _, p := q.ans82, q.para82 + fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(deleteDuplicates1(S2l(p.one)))) + } + fmt.Printf("\n\n\n") +}