Created
January 1, 2018 15:57
-
-
Save scriptnull/e2992207435c271619bddb1f20261c3f to your computer and use it in GitHub Desktop.
16 ms solution for https://leetcode.com/problems/remove-linked-list-elements/description/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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