day 3 第二章 链表

48 阅读1分钟

day 3 第二章 链表

\

day1 任务以及具体安排:docs.qq.com/doc/DUG9UR2…

day 2 任务以及具体安排:docs.qq.com/doc/DUGRwWX…

今日任务

\

  • 链表理论基础
  • 203.移除链表元素
  • 707.设计链表
  • 206.反转链表

\

详细布置

\

链表理论基础

\

建议:了解一下链接基础,以及链表和数组的区别

\

文章链接:programmercarl.com/%E9%93%BE%E…

\

203.移除链表元素

\

建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。

\

题目链接/文章讲解/视频讲解::programmercarl.com/0203.%E7%A7…


# 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]:
        while head !=None and head.val==val:
            head=head.next
        cur=head
        while cur!=None and cur.next !=None:
            if cur.next.val==val:
                cur.next=cur.next.next
            else:
                cur=cur.next
        return head

707.设计链表

\

建议: 这是一道考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点

\

题目链接/文章讲解/视频讲解:programmercarl.com/0707.%E8%AE…

\

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


class MyLinkedList:

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

    def get(self, index: int) -> int:
        if index >= 0 and index < self.count:
            cur = self.head
            for _ in range(index+1):
                cur = cur.next

            return cur.val
        else:
            return -1

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

    def addAtTail(self, val: int):
        self.addAtIndex(self.count, val)

    def addAtIndex(self, index: int, val: int) -> None:
        # if index<0:
        #     index=0
        # if index >= 0 and index < self.count:
        #     addNode = Node(val)
        #     self.count += 1
        #     pre, cur = None, self.head
        #     for _ in range(index + 1):
        #         pre, cur = cur, cur.next
        #     else:
        #         pre.next, addNode.next = addNode, cur
        if index < 0:
            index = 0
        elif index > self.count:
            return
        self.count +=1
        addNode = Node(val)
        pre, cur = None, self.head
        for _ in range(index + 1):
            pre, cur = cur, cur.next
        else:
            pre.next, addNode.next = addNode, cur
        

    def deleteAtIndex(self, index: int) -> None:
        if index >= 0 and index < self.count:
            pre, cur = None, self.head
            for _ in range(index + 1):
                pre, cur = cur, cur.next
            else:
                pre.next = cur.next
            self.count -=1

206.反转链表

\

建议先看我的视频讲解,视频讲解中对 反转链表需要注意的点讲的很清晰了,看完之后大家的疑惑基本都解决了。

题目链接/文章讲解/视频讲解:programmercarl.com/0206.%E7%BF…

# 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]:
        pre,cur=None,head
        
        while cur:
            temp= cur.next
            cur.next=pre
            pre,cur=cur,temp
        return pre