网站短信通知,网站添加地图导航,app模板制作软件免费下载,下载软件用哪个软件好题目
t. - 力扣#xff08;LeetCode#xff09;
给你一个链表的头节点 head 和一个整数 val #xff0c;请你删除链表中所有满足 Node.val val 的节点#xff0c;并返回 新的头节点 。
例1 输入#xff1a;head [1,2,6,3,4,5,6], val 6
输出#xff1a;[1,2,3,4,5]…题目
t. - 力扣LeetCode
给你一个链表的头节点 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
输出[]
思路
当需要删除的节点是头节点时需要单独考虑这时候设置一个虚拟的头节点将虚拟的头节点指向真实的头节点将头节点看作普通节点做同样的操作即可但最后返回的时候一定要返回虚拟头节点的next。 代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val0, nextNone):
# self.val val
# self.next next
class Solution:def removeElements(self, head: Optional[ListNode], val: int) - Optional[ListNode]:if not head:returnphead,cur ListNode(),ListNode()phead.next headcur pheadwhile cur.next:if cur.next.val val:cur.next cur.next.nextelse:cur cur.nextreturn phead.next 文章转载自: http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.dschz.cn.gov.cn.dschz.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.bynf.cn.gov.cn.bynf.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn