LC 203, 707, 206 First part of Linked List.

LC 203, 707, 206


First part of Linked List.

LC 209. Minimum Size Subarray Sum


203.移除链表元素

Logic

In order, we have:
p a r e n t → c u r → c u r . n e x t parent \rightarrow cur \rightarrow cur.next parentcurcur.next

  • Set a parent node, if the cur node shouldn’t be deleted, then we can set it as parent.
  • Upon deletion, we can just assign parent.next = cur.next, so cur in between is deleted.
# LC 203
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
    if not head:
        return head
    cur = head
    parent = None
    while cur:
        if cur.val != val:
            parent = cur
        else:
            if not parent:
                head = cur.next
            else:
                parent.next = cur.next
        cur = cur.next
    return head

707. Design Linked List


707.设计链表

Not really possible for me to finish in 1 hr, more practice is needed.

Logic

I used a double linked-list with a head node.

  • So I actually has a head node,
    • where head.next points to the really first elem.
    • and head.prev points to the back elem.
class MyLinkedList:
    def __init__(self, val=None, next=None, prev=None):
        self.val = val
        self.next = next
        self.prev = prev

    def get(self, index: int) -> int:
        if index < 0:
            return -1
        count = 0
        # start with the head
        cur: MyLinkedList = self.next
        while count < index and cur and cur.next:
            cur = cur.next
            count += 1
        if not cur or cur.val is None or count < index:
            return -1
        return cur.val

    def addAtHead(self, val: int) -> None:
        new_node = MyLinkedList(val=val, next=self.next)
        if self.next is not None:
            self.next.prev = new_node
        self.next = new_node
        if self.prev is None:
            self.prev = new_node

    def addAtTail(self, val: int) -> None:
        tail = self.prev
        new_node = MyLinkedList(val=val, next=None, prev=tail)
        if tail:
            tail.next = new_node
        else:
            self.next = new_node
        self.prev = new_node

    def addAtIndex(self, index: int, val: int) -> None:
        if index < 0:
            return
        count = 0
        cur: MyLinkedList = self
        while count < index and cur and cur.next:
            cur = cur.next
            count += 1
        if count < index:
            return
        new_node = MyLinkedList(val=val, next=cur.next, prev=cur)
        if cur.next:
            cur.next.prev = new_node
        cur.next = new_node
        if cur == self.prev:
            self.prev = new_node

    def deleteAtIndex(self, index: int) -> None:
        if index < 0:
            return
        count = 0
        cur: MyLinkedList = self
        while count < index and cur and cur.next:
            cur = cur.next
            count += 1
        if count < index or not cur:
            return
        if cur.next and cur.next.next:
            cur.next = cur.next.next
            cur.next.prev = cur
        elif cur.next:
            cur.next = None
            self.prev = cur

    # for the sake of debugging
    def __repr__(self) -> str:
        ret = "head"
        cur = self.next
        while cur and cur.val is not None:
            ret += f"->{str(cur.val)}"
            cur = cur.next
        return ret

206. Reverse Linked List


206.反转链表

Logic:

  • Create a new Linked List from the end.
  • Or we can reverse the link using another ptr.
# 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 reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        cur = head
        ret = None
        if cur is not None:
            ret = ListNode(cur.val)
            while cur.next is not None: 
                cur = cur.next
                new_node = ListNode(cur.val, ret)
                ret = new_node
        return ret

Summary:


  • Overall, the linked list part is not yet hard for me now, but the design for the linked list is crucial, and the condition for None check is necessary.

  • Total time: 4-5 hrs ish.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值