赞
踩
python定义:
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
删除节点:
添加节点:
性能分析:
在这里插入代码片
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy_head = ListNode(next=head)
cur = dummy_head
while cur.next:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return dummy_head.next
相当于啥也没给你,全在里面操作。
class ListNode: # 单链表 def __init__(self, val=0, next=None): self.val = val self.next = next class MyLinkedList: def __init__(self): self.dummy_head = ListNode() # 虚拟头 self.count = 0 # 节点数 def get(self, index: int) -> int: if 0 <= index < self.count: cur = self.dummy_head for i in range(index + 1): cur = cur.next return cur.val else: return -1 def addAtHead(self, val: int) -> None: new_list = ListNode(val) new_list.next = self.asummy_head.next self.dummy_head.next = new_list self.count += 1 def addAtTail(self, val: int) -> None: new_list = ListNode(val) cur = self.dummy_head while cur.next: cur = cur.next cur.next = new_list self.count += 1 def addAtIndex(self, index: int, val: int) -> None: if index < 0: self.addAtHead(val) elif 0 <= index < self.count: new_list = ListNode(val) cur = self.dummy_head for i in range(index): cur = cur.next new_list.next = cur.next cur.next = new_list self.count += 1 elif index == self.count: self.addAtTail(val) def deleteAtIndex(self, index: int) -> None: if 0 <= index < self.count: cur = self.dummy_head for i in range(index): cur = cur.next cur.next = cur.next.next self.count -= 1
双链表增加了获取index 和直接增加节点的功能。
class LinkNode: # 双链表 def __init__(self, val = 0, prev = None, next = None): self.prev = prev self.next = next self.val = val class MyLinkedList: def __init__(self): self.dummy_head = LinkNode() self.dummy_tail = LinkNode() self.dummy_head.next, self.dummy_tail.prev = self.dummy_tail, self.dummy_head self.count = 0 def get_node(self, index): if index < self.count // 2: cur = self.dummy_head for i in range(index + 1): cur = cur.next return cur else: cur = self.dummy_tail for i in range(self.count - index): cur = cur.prev return cur def get(self, index: int) -> int: if 0 <= index < self.count: node = self.get_node(index) return node.val else: return -1 def addAtHead(self, val: int) -> None: self.add_data(val, self.dummy_head, self.dummy_head.next) def addAtTail(self, val: int) -> None: self.add_data(val, self.asummy_tail.prev, self.dummy_tail) def addAtIndex(self, index: int, val: int) -> None: if index <= 0: self.add_data(val, self.dummy_head, self.dummy_head.next) elif self.count < index: return elif 0 < index < self.count: node = self.get_node(index) self.add_data(val, node.prev, node) elif index == self.count: self.add_data(val, self.dummy_tail.prev, self.dummy_tail) def add_data(self, val, prev_node, next_node): new_node = LinkNode(val) prev_node.next, next_node.prev = new_node, new_node new_node.prev, new_node.next = prev_node, next_node self.count += 1 def deleteAtIndex(self, index: int) -> None: if 0 <= index < self.count: node = self.get_node(index) node.prev.next, node.next.prev = node.next, node.prev self.count -= 1
pre = cur 这一步相当于把pre移到cur的位置,原来的节点保持不变
class Solution: # 迭代
def reverseList(self, head: ListNode) -> ListNode:
cur = head
pre = None
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
self.dummy_head = ListNode()
self.dummy_head.next = head
slow, fast = self.dummy_head, self.dummy_head
for i in range(n+1):
fast = fast.next
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
# Tip: 这里要返回self.dummy_head.next,而不是返回head,因为当链表的长度只有1并且删除倒数第一个时head还是存在的。
return self.dummy_head.next
即球两个链表交点节点的指针,注意这里交点不是数值相同,而是指针相同。
方法:求出两个链表的长度,并求出两个链表长度的差值,然后让curA移动到和curB末尾对齐的位置,如图:
比较curA和curB是否相同,如果不相同吗,同时往后移动,如果遇到curA==curB则为碰到交点,否则循环退出返回空指针。
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: curA = headA curB = headB def get_lens(head): count = 0 while head: count += 1 head = head.next return count len_A = get_lens(headA) len_B = get_lens(headB) if len_A > len_B: for i in range(len_A - len_B): curA = curA.next else: for i in range(len_B - len_A): curB = curB.next while curA: if curA == curB: return curB curA = curA.next curB = curB.next return None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
curA = head
curB = slow
while curA != curB:
curA = curA.next
curB = curB.next
return curA
return None
感谢阅读
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。