type ListNode struct { Next *ListNode Value int }
func RemoveNodeNormal(head *ListNode, value int) *ListNode { var prev *ListNode for cur := head; cur != nil; cur = cur.Next { if cur.Value == value { if prev != nil { prev.Next = cur.Next } else { head = cur.Next } } else { prev = cur } } return head }
if 是 || !不是 { 請繼續往下看 }
func RemoveNodeReplace(head *ListNode, value int) *ListNode { cur := head for cur != nil && cur.Value == value { if cur.Next == nil { return nil } cur.Value = cur.Next.Value cur.Next = cur.Next.Next } for cur != nil { if cur.Next != nil && cur.Next.Value == value { cur.Next = cur.Next.Next } else { cur = cur.Next } } return head }
func RemoveNode(head *ListNode, value int) *ListNode { for cur := &head; *cur != nil; { if (*cur).Value == value { *cur = (*cur).Next } else { cur = &(*cur).Next } } return head }
package main import ( "fmt" ) type ListNode struct { Next *ListNode Value int } func RemoveNodeNormal(head *ListNode, value int) *ListNode { var prev *ListNode for cur := head; cur != nil; cur = cur.Next { if cur.Value == value { if prev != nil { prev.Next = cur.Next } else { head = cur.Next } } else { prev = cur } } return head } func RemoveNodeReplace(head *ListNode, value int) *ListNode { cur := head for cur != nil && cur.Value == value { if cur.Next == nil { return nil } cur.Value = cur.Next.Value cur.Next = cur.Next.Next } for cur != nil { if cur.Next != nil && cur.Next.Value == value { cur.Next = cur.Next.Next } else { cur = cur.Next } } return head } func RemoveNode(head *ListNode, value int) *ListNode { for cur := &head; *cur != nil; { if (*cur).Value == value { *cur = (*cur).Next } else { cur = &(*cur).Next } } return head } func ArrayToLink(nums []int) *ListNode { if len(nums) == 0 { return nil } head := &ListNode{ Value: nums[0], } tail := head for i := 1; i < len(nums); i++ { tail.Next = &ListNode{ Value: nums[i], } tail = tail.Next } return head } func LinkToArray(head *ListNode) []int { var array []int for ; head != nil; head = head.Next { array = append(array, head.Value) } return array } func ArrayEqual(nums1, nums2 []int) bool { if len(nums1) != len(nums2) { return false } for i := 0; i < len(nums1); i++ { if nums1[i] != nums2[i] { return false } } return true } func main() { tests := []struct { nums []int value int res []int }{ { []int{}, 1, []int{}, }, { []int{1}, 1, []int{}, }, { []int{1, 2}, 1, []int{2}, }, { []int{1, 2}, 2, []int{1}, }, { []int{1, 2, 1, 3, 1, 4, 1, 1, 1, 1, 1}, 1, []int{2, 3, 4}, }, { []int{1, 2, 3, 2, 4, 2}, 2, []int{1, 3, 4}, }, { []int{3, 1, 3, 2, 3, 4, 3}, 3, []int{1, 2, 4}, }, { []int{4, 1, 4, 2, 4, 3, 4, 4, 4, 4, 4}, 4, []int{1, 2, 3}, }, } for _, test := range tests { head := ArrayToLink(test.nums) head = RemoveNode(head, test.value) array := LinkToArray(head) fmt.Println(ArrayEqual(array, test.res)) } }
若是你對算法感興趣,能夠看看我刷的LeetCode:https://github.com/chentaihan/leetcode (求贊)git
若是你對算法感興趣,能夠看看我刷的LeetCode:https://github.com/chentaihan/leetcode (求贊)github
若是你對算法感興趣,能夠看看我刷的LeetCode:https://github.com/chentaihan/leetcode (求贊)golang