181 字
1 分钟
LeetCode-203.移除链表元素
链表
给你一个链表的头节点
head和一个整数val,请你删除链表中所有满足Node.val == val的节点,并返回 新的头节点 。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6输出:[1,2,3,4,5]示例 2:
输入:head = [], val = 1输出:[]示例 3:
输入:head = [7,7,7,7], val = 7输出:[]
自己的解法
手撕,链表的第一题
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func removeElements(head *ListNode, val int) *ListNode { if head == nil { return head }
for head != nil && head.Val == val { head = head.Next }
current := head for current != nil && current.Next != nil { if current.Next.Val != val { current = current.Next }else { current.Next = current.Next.Next } }
return head} LeetCode-203.移除链表元素
https://sheep44044.github.io/posts/算法/链表/leetcode-203移除链表元素/
