代码随想录算法训练营第三天| 203.移除链表元素 707.设计链表 206.反转链表

本文介绍了链表的基本操作,包括使用虚拟头结点进行删除元素的方法,以及链表的反转,提到了双指针和递归两种反转链表的实现方式。

链表的基本结构

  • 直接使用原来的链表来进行删除操作。
  • 设置一个虚拟头结点在进行删除操作。
     Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, val=0, next=None):
    #         self.val = val
    #         self.next = next
    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=cur.next.next
                else:
                    cur=cur.next
            return dummy_head.next

    707.设计链表

  • #伪代码
    1.获取第n个节点的值
    2.头部插入节点
    3.尾部插入节点
    4.第n个节点前插入节点
    5.删第n个节点
    
    首先设置虚拟头节点
    n<0  n>size-1
    
    问题1:
    cur=dummy_head.next;
    while(n){
      cur=cur.next;
      n--}
    return cur.val
    
    问题2:
    newnode=new node()
    (dummyhead.next=newnode
    newnode.next 这个有问题 先右边,再左边)
    
    newnode.next=dummy.next
    dummy.next=newnode
    size++
    
    问题3:
    newnode=new node()
    cur当前指针要指向尾部的前一个
    cur=dummyhead
    while(cur.next!=Null){
      cur=cur.next
     }
    cur.next=newnode
    
    问题4:
    要知道第n-1个节点 第n个节点一定是cur.next
    cur=dummyhead
    while(n){
      cur=cur.next
      n--;
    }
    newnode=cur.next
    cur.next=newnode;
    size++
    
    问题5:
    cur=dummyhead
    while(n){
      cur=cur.next;
      n--;}
    cur.next=cur.next.next
    size--;
    
    
    
    
    
    

    206.反转链表

  • #伪代码
    
    
    #双指针的解法
    一个指针指向头节点 cur 
    一个指针 pre cur的前面
    改变链表的连接方向
    cur=head
    pre=NULL
    
    while(cur){
      temp=cur.next  #临时存放正向时cur的next
      cur.next=pre
      pre=cur   #必须先放pre
      cur=temp
    }
    return pre;
    
    
    #递归
    比较晦涩难懂
    reverse(cur,pre){
      if(cur=NULL)
         return pre;
       temp=cur.next
       cur.next=pre
      reverse(temp,cur)
       }
    
    
    
    reverselist(head){
       return  reverse(head,NULL);
    }
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, val=0, next=None):
    #         self.val = val
    #         self.next = next
    class Solution:
        def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
           1. #双指针方法
            cur=head
            pre=None
            while(cur!=None):
                temp=cur.next
                cur.next=pre
                pre=cur
                cur=temp
            return pre
    
    
    2.  def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
            #递归
            def reverse(cur,pre):
                if(cur==None):
                    return pre
                temp=cur.next
                cur.next=pre
                return reverse(temp,cur)
            return reverse(head,None)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值