Skip to content

Instantly share code, notes, and snippets.

@scriptnull
Created January 1, 2018 15:57
Show Gist options
  • Select an option

  • Save scriptnull/e2992207435c271619bddb1f20261c3f to your computer and use it in GitHub Desktop.

Select an option

Save scriptnull/e2992207435c271619bddb1f20261c3f to your computer and use it in GitHub Desktop.
// solves https://leetcode.com/problems/remove-linked-list-elements/description/
// runtime is 16ms
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeElements(head *ListNode, val int) *ListNode {
if head == nil {
return nil
}
for head != nil && head.Val == val {
head = head.Next
}
if head == nil {
return nil
}
p := head
for p.Next != nil {
if p.Next.Val == val {
p.Next = p.Next.Next
} else {
p = p.Next
}
}
return head
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment