Delete the list, would you?

Keywords: Go github

Delete all elements in the single-chain list with a value equal to XXX
I inadvertently saw an unusual implementation and found it interesting, so I implemented it myself. The code is really simple and clear, and it runs fast!
Okay, now think in your mind, how would you do that?It's so simple. After 5 seconds, you think about a solution, and you decide to verify your thinking. Keep looking down
 
Define the chain table node structure as follows:
type ListNode struct {
   Next  *ListNode
   Value int
}

 

1: Most common ideas
Defines a variable prev that holds the previous node. When the current node cur is found to be equal to the target value, prev.next = cur.next is iterated through, skipping all nodes whose node value is equal to the target value, i.e. deleting all nodes whose value is XXX. The golang code implements the following:
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
}
It's so simple that I don't explain anything, but complete code and a simple single test are attached
Is that the way you think?
if is ||!Not {
    Please keep looking down  
}
 
2: Second Common Ideas
If I find that the current node value is equal to the target value, then override the current node value with the value of the next node, and the next node of the current node points down to the next node.The code is implemented as follows:
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
}

 

 
3:linus Favorite Code
linus said that the code should be written like this, I will not explain anything, you product, you fine!!
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
}

 

 
The complete code is as follows. You can try it out by running:
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))
    }
}

 

If you're interested in algorithms, take a look at the LeetCode I brushed: https://github.com/chentaihan/leetcode (Praise)

If you're interested in algorithms, take a look at the LeetCode I brushed: https://github.com/chentaihan/leetcode (Praise)

If you're interested in algorithms, take a look at the LeetCode I brushed: https://github.com/chentaihan/leetcode (Praise)

 

 

Posted by Thierry on Wed, 18 Dec 2019 23:35:13 -0800