ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

周报1.2

2022-02-07 14:59:19  阅读:76  来源: 互联网

标签:index head val 1.2 self cur next 周报


题目:

第一天:707. 设计链表

第二天:206. 反转链表

收获,在使用head时,要先将self.head赋值给head,才能对head进行操作,在add和delete之后要改变链表的长度,range()函数是从0开始的

class Node:

    def __init__(self, val):

        self.val = val

        self.next = None

class MyLinkedList:

    def __init__(self):

        self.head = Node(0)

        self.size = 0 

    def get(self, index: int) -> int:

        if index < 0 or index >= self.size:

            return -1

        head = self.head

        #返回的是第i个,range()是从0开始算的

        for i in range(index + 1):

            head = head.next

        return head.val

    def addAtHead(self, val: int) -> None:

        new = Node(val)

        temp = self.head.next

        self.head.next = new

        new.next = temp

        self.size += 1

    def addAtTail(self, val: int) -> None:

        new = Node(val)

        head = self.head

        while head.next != None:

            head = head.next

        head.next = new

        self.size += 1


 

    def addAtIndex(self, index: int, val: int) -> None:

        '''

        if index > self.number: return

        elif index = n: 

            self.number.next = val 

        else:

            self.

        '''

        new = Node(val)

        head = self.head

        if index == 0:

            self.addAtHead(val)

            return

        elif index == self.size:

            self.addAtTail(val)

            return

        elif index < 0 or index > self.size:

            return

        for i in range(index):

            head = head.next

        temp = head.next

        head.next = new

        new.next = temp

        self.size += 1


 

    def deleteAtIndex(self, index: int) -> None:

        '''

        if i == index - 1 and 0 < index < len(self):

            self.next = self.next.next

        elif index < 0:

            self.

        '''

        if index >= self.size:

            return

        elif index < 0:

            index = 0

        head = self.head

        for i in range(index):

            head = head.next

        head.next = head.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)

2.在反转的过程中,首先要用tmp保留整个链表的结构,以便遍历。在循环中让cur.next指向pre,使得cur永远是新添加的结点

class Solution:

    def reverseList(self, head: ListNode) -> ListNode:

        cur = head

        pre = None

        while (cur != None):

            temp = cur.next     #将原本的cur.next先存起来

            cur.next = pre      #反转

            pre = cur           #让把遍历到的cur链表赋给pre,

            cur = temp          #遍历下一个结点

        return pre  

标签:index,head,val,1.2,self,cur,next,周报
来源: https://blog.csdn.net/Allen_Ming/article/details/122766444

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有