第三天| 203.移除链表元素 , 707.设计链表, 206.反转链表

本文介绍了三种链表操作:移除链表元素,设计链表类以及反转链表。提供了两种删除元素的方法,一种是直接修改指针,另一种是利用虚拟头节点。设计链表时需注意更新size和处理空链表。反转链表可通过双指针或递归实现,文中更倾向于双指针法。
摘要由CSDN通过智能技术生成

今天是链表专题,三道题中707这道题是一道OOD相较其他两题,难度稍大

203.移除链表元素

这道题没有太难,简单来说只要运用a.next=a.next.next即可

Way1:

这种方法要考虑到两种情况,一种是如果delate的是头则直接移动head就行,另一种就是正常便利删除即可

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        while head and head.val==val:
            head=head.next
        cur=head
        while cur:
            while cur.next and cur.next.val==val:
                cur.next=cur.next.next
            cur=cur.next
        return head

Way2:

这种方法就是类似于循环不变量,即设计一个空的头节点,直接遍历下去即可

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        dummy_head = ListNode(next=head) 
        cur = dummy_head
        while cur.next!=None :
            if cur.next.val == val:
                cur.next = cur.next.next 
            else:
                cur = cur.next
        return dummy_head.next

707.设计链表,

这道题是一道典型的ood的题,要全方面的设计,主要的容易出错的在于两点: 一点是要记得更新size,还是有一点是head是空的,head,next才是开始的node,方法上没有什么难点,遍历的过程既可以用for也可以用while循环

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

class MyLinkedList:

    def __init__(self):
        self.head=Node()
        self.size=0

    def get(self, index: int) -> int:
        if self.size<=index:
            return -1
        cur=self.head.next
        for _ in range(index):
            cur=cur.next
        return cur.val


    def addAtHead(self, val: int) -> None:
        new=Node(val)
        new.next=self.head.next
        self.head.next=new
        self.size+=1
        

    def addAtTail(self, val: int) -> None:
        new=Node(val)
        cur=self.head
        for _ in range(self.size):
            cur=cur.next
        cur.next=new
        self.size += 1


    def addAtIndex(self, index: int, val: int) -> None:
        if index>self.size:
            return 
        if index==self.size:
            self.addAtTail(val)
            return 
        new=Node(val)
        cur=self.head
        for _ in range(index):
            cur=cur.next
        new.next=cur.next
        cur.next=new
        self.size+=1

    def deleteAtIndex(self, index: int) -> None:
        if index>=self.size:
            return
        cur=self.head
        for _ in range(index):
            cur=cur.next
        cur.next=cur.next.next
        self.size-=1

206.反转链表

就是考虑如何翻转.关键的就两步一个是将cur,next=pre 一个是pre=next,剩下的就是方法的变通了

Way1:

就是单纯的two pointer, 不断的循环移动两个指针的位置,但是移动的timing要注意一下

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        node=head
        pre=None
        while node:
            tmp=node.next
            node.next=pre
            pre=node
            node=tmp
        return pre

Way2:

除了用双指针外,还可以用recursion来替代其中一个指针,非常类似于之前的用用for循环去顺序改变指针的index位置

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def reverse(pre,cur):
            if not cur:
                return pre                
            tmp = cur.next
            cur.next = pre
            return reverse(cur,tmp)       
        return reverse(None,head)

个人还是更喜欢two pointer的方法,因为更加的直接,而且据说现在的面试相对于之前不倾向于考recursion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值