代码随想录刷题day3

学习目标:

  1. 移除链表元素
    1)使用虚拟头节点的思想,使链表中每一个位置的移除都统一。但是要新建一个current指针用来遍历链表,最好不要动虚拟头节点(它要用来return)。新建节点的时候要注意语法,不像java一样需要new。
    2) cur = cur.next应写在else中,因为改变cur.next是每轮循环需要做的。
代码:
# 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
        """
        virtualHead = ListNode()
        # cur = ListNode()没有必要
        virtualHead.next = head
        cur = virtualHead
        while cur.next != None:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next#注意这一行应该单独写在else, 因为其实在每一次循环中,我们只想更新cur.next的值,而在if中已经做了改变。
        return virtualHead.next
    1. 设计链表
      1)错误点主要有三,一是对插入尾节点时候,要借助next指针,不能指向最后一个null再赋值,单纯让两个指针相等,相当于让他们所指向的东西相同,但无法起到链表的连接作用!!!真的debug了很久wtf。
      2)对于插入节点的index判断,当index == 0 or == self.size的时候,分别调用插入头节点和尾节点的函数。
      3)这里的head所代表的和上一道题不同。这里的是在头节点之前的null节点,因为初始化的时候需要用到它,所以用法要和上一题有区分。
代码
class Node(object):
    def __init__(self, x=0):
        self.val = x
        self.next = None
class MyLinkedList(object):

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


    def get(self, index):
        """
        :type index: int
        :rtype: int
        """
        
        if index >= self.size or index < 0:
            return -1
        cur = self.head.next
        while index > 0:
            cur = cur.next
            index -= 1
        return cur.val


    def addAtHead(self, val):
        """
        :type val: int
        :rtype: None
        """
        temp = Node(val)
        temp.next = self.head.next
        self.head.next = temp
        self.size += 1



    def addAtTail(self, val):#错在这了 重点看
        """
        :type val: int
        :rtype: None
        """
        # temp = Node(val)
        # cur = self.head.next
        # while cur:
        #     cur = cur.next
        # cur = temp
        # self.size += 1
        new_node = Node(val)
        cur = self.head
        while(cur.next):
            cur = cur.next
        cur.next = new_node
        self.size += 1



    def addAtIndex(self, index, val):
        """
        :type index: int
        :type val: int
        :rtype: None
        """
        if index < 0 or index > self.size:
            return 
        elif index == 0:
            self.addAtHead(val)
        elif index == self.size:
            self.addAtTail(val)
        else:
            cur = self.head.next
            while index > 1:
                cur = cur.next
                index -= 1
            temp = Node(val)
            temp.next = cur.next
            cur.next = temp
            self.size += 1


    def deleteAtIndex(self, index):
        """
        :type index: int
        :rtype: None
        """
        if index < 0 or index >= self.size:
            return 
        else:
            cur = self.head
            while index > 0:
                cur = cur.next
                index -= 1
            cur.next = cur.next.next
            self.size -= 1




# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
  1. 206反转链表
    1)双指针思路:比较简单,用两个指针从前到后挨个翻转。pre指针在后,cur指针在前,用来形成一个反转场景。注意赋值的顺序 cur.next = pre。根本上来说:在翻转前应该先记录cur的下一个节点,以防找不到。
    2)递归思路:完全跟着双指针思路写下来即可。每一次调用递归函数,都相当于把pre,cur向后移动一位。
    3)总结:这两种写法的根本原理还是双指针,只不过第一种通过while来实现的,第二种是通过递归函数实现的。
双指针写法:
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        pre = None
        cur = head
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre
递归写法:先定义函数再调用
 def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        def reverseTry(cur, pre):
            if cur == None:
                return pre
            temp = cur.next
            cur.next = pre
            return reverseTry(temp, cur)
            
        return reverseTry(head, None)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值