代碼隨想錄算法訓(xùn)練營(yíng)第三天|203.移除鏈表元素 、707.設(shè)計(jì)鏈表疼阔、206.反轉(zhuǎn)鏈表

203. 移除鏈表元素 - 力扣(LeetCode)

之前沒(méi)接觸過(guò)鏈表盲憎,直接看講解

解題思路

有兩種嗅骄,單獨(dú)處理頭節(jié)點(diǎn),或者設(shè)置虛擬頭節(jié)點(diǎn)饼疙,讓頭節(jié)點(diǎn)作為普通節(jié)點(diǎn)

方法一 使用原鏈表

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        while head :
            if head.val == val:
                head = head.next
            else:
                break

        current = head

        while current and current.next is not None:
            if current.next.val == val:
                current.next = current.next.next
            else:
                current = current.next

        return head

方法二 虛擬頭節(jié)點(diǎn)

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        dummy_head = ListNode()
        dummy_head.next = head

        current = dummy_head

        while current.next:
            if current.next.val == val:
                current.next = current.next.next
            else:
                current = current.next

        return dummy_head.next

707. 設(shè)計(jì)鏈表 - 力扣(LeetCode)

單鏈表

class ListNode():      
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class MyLinkedList(object):

    def __init__(self):
        self.dummy_head = ListNode()
        self.size = 0

    def get(self, index):
        """
        :type index: int
        :rtype: int
        """
        if index < 0 or index >= self.size:
            return -1
        
        current = self.dummy_head.next
        for i in range(index):
            current = current.next
            
        return current.val

    def addAtHead(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.dummy_head.next = ListNode(val, self.dummy_head.next)
        self.size += 1


    def addAtTail(self, val):
        """
        :type val: int
        :rtype: None
        """
        current = self.dummy_head
        while current.next:
            current = current.next
        current.next = ListNode(val)
        self.size += 1

    def addAtIndex(self, index, val):
        """
        :type index: int
        :type val: int
        :rtype: None
        """
        if index < 0 or index > self.size:
            return
        
        current = self.dummy_head
        for i in range(index):
            current = current.next
        current.next = ListNode(val, current.next)
        self.size += 1

    def deleteAtIndex(self, index):
        """
        :type index: int
        :rtype: None
        """
        if index < 0 or index >= self.size:
            return

        current = self.dummy_head
        for i in range(index):
            current = current.next
        current.next = current.next.next
        self.size -= 1

206. 反轉(zhuǎn)鏈表 - 力扣(LeetCode)

雙指針?lè)?/h2>
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        cur = head
        pre = None

        while cur:
            temp = cur.next #保存cur下一個(gè)節(jié)點(diǎn)
            cur.next = pre # 反轉(zhuǎn)

            pre = cur  #面向?qū)ο?            cur = temp           

        return pre

遞歸

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        return self.reverseNode(head, None)
    
    def reverseNode(self, cur, pre):
        if cur is None:
            return pre

        temp = cur.next
        cur.next = pre
        return self.reverseNode(temp, cur)
               
  • 注意類的寫(xiě)法
  • 遞歸還需練習(xí)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市窑眯,隨后出現(xiàn)的幾起案子屏积,更是在濱河造成了極大的恐慌,老刑警劉巖磅甩,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炊林,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡卷要,警方通過(guò)查閱死者的電腦和手機(jī)渣聚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)僧叉,“玉大人奕枝,你說(shuō)我怎么就攤上這事”氡辏” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵掷豺,是天一觀的道長(zhǎng)捞烟。 經(jīng)常有香客問(wèn)我,道長(zhǎng)当船,這世上最難降的妖魔是什么题画? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮德频,結(jié)果婚禮上苍息,老公的妹妹穿的比我還像新娘。我一直安慰自己壹置,他們只是感情好竞思,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著钞护,像睡著了一般盖喷。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上难咕,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天课梳,我揣著相機(jī)與錄音距辆,去河邊找鬼。 笑死暮刃,一個(gè)胖子當(dāng)著我的面吹牛跨算,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播椭懊,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼诸蚕,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了灾搏?” 一聲冷哼從身側(cè)響起挫望,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎狂窑,沒(méi)想到半個(gè)月后媳板,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡泉哈,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年蛉幸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丛晦。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奕纫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出烫沙,到底是詐尸還是另有隱情匹层,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布锌蓄,位于F島的核電站升筏,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏瘸爽。R本人自食惡果不足惜您访,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望剪决。 院中可真熱鬧灵汪,春花似錦、人聲如沸柑潦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)渗鬼。三九已至担锤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間乍钻,已是汗流浹背肛循。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工铭腕, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人多糠。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓累舷,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親夹孔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子被盈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容