203:移除链表元素
- Given the
head of a linked list
and an integerval
- 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