移除链表元素_设计链表

本文探讨了如何在链表中移除指定值的节点,并介绍了设计链表的方法,包括初始化、获取节点值、添加节点以及删除节点等操作。
摘要由CSDN通过智能技术生成

203:移除链表元素

  • Given the head of a linked list and an integer val
  • remove all the nodes of the linked list that has Node.val == val, and return the new head.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
def remove_Linklist_element(head, val):
    while head is not None and head.val == val:
        head = head.next
    if head is None:
        return head
        
    p = head
    while p is not None and p.next is not None:
            
        if p.next.val != val:
            p = p.next
        else:
            p.next = p.next.next
    if p is not None and p.next is None:
        return head
    return head
vals_ = [1,2,6,3,4,5,6]
temp = []
for i in vals_:
    temp.append(ListNode(i))
for i in range(len(temp)-1):
    temp[i].next = temp[i+1]

for i in range(len(temp)):
    print(temp[i].val, temp[i].next)
1 <__main__.ListNode object at 0x113fd6ac0>
2 <__main__.ListNode object at 0x113fd6cd0>
6 <__main__.ListNode object at 0x113fd6460>
3 <__main__.ListNode object at 0x113fd6b80>
4 <__main__.ListNode object at 0x113fda430>
5 <__main__.ListNode object at 0x113fda160>
6 None
ans = remove_Linklist_element(temp[0], 6)
while ans is not None:
    print(ans.val)
    ans = ans.next
1
2
3
4
5

707:设计链表

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

MyLinkedList() Initializes the MyLinkedList object.

int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.

void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.

void addAtTail(int val) Append a node of value val as the last element of the linked list.

void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.

void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.

class Node:
    def __init__(self, val, next = None):
        self.val = val
        self.next = next
        
class MyLinkedList:
    def __init__(self):
        # 虚拟头节点
        self.head = Node(0)
        self.count = 0
        
    def get(self, index: int) -> int:
        p = self.head
        if index >= self.count:
            return -1
        i = 0
        while i <= index:
            p = p.next
            i+=1
        return p.val         

    def addAtHead(self, val: int) -> None:
        p = Node(val)
        p.next = self.head.next
        self.head.next = p
        self.count += 1

    def addAtTail(self, val: int) -> None:
        p = self.head
        while p.next is not None:
            p = p.next
        p.next = Node(val)
        self.count += 1
    def addAtIndex(self, index: int, val: int) -> None:
        if index > self.count:
            return
        i = 0
        p = self.head
        while i < index:
            p = p.next
            i += 1
        t = Node(val)
        t.next = p.next
        p.next = t
        self.count += 1
        
    def deleteAtIndex(self, index: int) -> None:
        p = self.head
        if index >= self.count:
            return
        i = 0
        while i < index:
            p = p.next
            i+=1
        p.next = p.next.next
        self.count -= 1
obj = MyLinkedList()
obj.addAtHead(1)
obj.addAtTail(3)
obj.addAtIndex(1, 2)
param_1 = obj.get(1)
print(param_1)
obj.deleteAtIndex(1)
param_2 = obj.get(1)
print(param_2)
2
3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值