Leetcode 203. 移除链表元素 (每日一题 20210914)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 203. 移除链表元素 (每日一题 20210914)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給你一個鏈表的頭節點 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
輸出:[]鏈接:https://leetcode-cn.com/problems/remove-linked-list-elementsclassSolution:def removeElement(self,head:ListNode, val:int)->ListNode:dummpy = ListNode(0)dummpy.next = headcur = dummpywhile cur is not None:if cur.next and cur.next.val = val:cur.next = cur.next.nextelse:cur = cur.nextreturn dummpy.next
總結
以上是生活随笔為你收集整理的Leetcode 203. 移除链表元素 (每日一题 20210914)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 217. 存在重复元素
- 下一篇: Leetcode 剑指 Offer 09