def merge(self, l1: ListNode, l2: ListNode): if not l1: return l2 if not l2: return l1 if l1.val < l2.val: l1.next = self.merge(l1.next, l2) return l1 l2.next = self.merge(l1, l2.next) return l2
def isPalindrome(self, head: ListNode) -> bool: lenh = 0 p = head # 链表长度 while p: lenh += 1 p = p.next # 后半部分 mid = (lenh + 1) // 2 left, rightp = head, head for _ in range(mid): rightp = rightp.next # 后半部分翻转 dumpy = ListNode(0) while rightp: tmp = rightp.next rightp.next = dumpy.next dumpy.next = rightp rightp = tmp right = dumpy.next #回文判断 while right: if left.val != right.val: return False left = left.next right = right.next return True
def reverseList(self, head: ListNode) -> ListNode: dumpy = ListNode(0) while head: tmp = head head = head.next tmp.next = dumpy.next dumpy.next = tmp return dumpy.next
def reverseList(self, head: ListNode) -> ListNode: dumpy = ListNode(0) first = head while head: head = head.next first.next = dumpy.next dumpy.next = first first = head return dumpy.next
def reverseList(self, head: ListNode) -> ListNode: res = ListNode(0) p = head while head: head = head.next p.next = res.next res.next = p p = head return res.next
def deleteNode(self, head: ListNode, val: int) -> ListNode: dumpy = ListNode(0) dumpy.next = head p = dumpy while p.next: if p.next.val == val: p.next = p.next.next break p = p.next return dumpy.next
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: dumpy = ListNode(0) dumpy.next = head p = dumpy for _ in range(n): head = head.next while head: head = head.next p = p.next p.next = p.next.next return dumpy.next
def removeElements(self, head: ListNode, val: int) -> ListNode: # 哨兵结点 dumpy = ListNode(0) dumpy.next = head prev, curr = dumpy, head while curr: if curr.val == val: prev.next = curr.next else: prev = curr curr = curr.next return dumpy.next
def rotateRight(self, head: ListNode, k: int) -> ListNode: if not head: return head p1 = head lenh = 0 while p1.next: p1 = p1.next lenh += 1 lenh += 1 k %= lenh if k == 0: return head dumpy = ListNode(0) dumpy.next = head p2 = head for _ in range(lenh - k - 1): p2 = p2.next dumpy.next = p2.next p1.next = head p2.next = None return dumpy.next
def removeNthFromEnd(head,n): dummy = ListNode(0) dummy.next = head fast = dummy while n: fast = fast.next n -= 1 listprint(fast) slow = dummy while fast.next: fast = fast.next slow = slow.next slow.next = slow.next.next return dummy.next
def deleteDuplicates(self, head: ListNode) -> ListNode: if not head: return head dumpy = ListNode(0) dumpy.next = head p1, p2 = head, head while p2.next: if p2.next.val == p1.val: p2 = p2.next continue p2 = p2.next p1.next = p2 p1 = p2 p1.next = p2.next return dumpy.next
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: dumpy = ListNode(0) dumpy.next = l1 p = dumpy while l1 and l2: if l1.val <= l2.val: l1 = l1.next p = p.next else: p.next = l2 p = p.next l2 = l2.next p.next = l1 if l2: p.next = l2 return dumpy.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: dumpy = ListNode(0) dumpy.next = l1 l = dumpy ans = 0 while l1 or l2: a = l1.val if l1 else 0 b = l2.val if l2 else 0 ans += a + b ans, mod = divmod(ans, 10) l.next = ListNode(mod) l = l.next if l1: l1 = l1.next if l2: l2 = l2.next if ans > 0: l.next = ListNode(1) return dumpy.next
def swapPairs(head): if not head or not head.next: return head dummynode = ListNode(0) dummynode.next = head pre = dummynode first = head second = head.next while first and second: pre.next = second nex = second.next second.next = first first.next = nex pre = first if first.next: second = first.next.next first = first.next else: break return dummynode.next
def deleteDuplicates(self, head: ListNode) -> ListNode: if not head: return head dumpy = ListNode(0) dumpy.next = head p1, p2, p3 = dumpy, head, head count = 0 while p3.next: if p3.next.val == p2.val: p3 = p3.next count += 1 continue p3 = p3.next p2 = p3 if count > 0: p1.next = p3 else: p1 = p1.next count = 0 if count > 0: p1.next = p3.next return dumpy.next
n -= 1 listprint(fast) slow = dummy while fast.next: fast = fast.next slow = slow.next slow.next = slow.next.next return dummy.next e1 = ListNode(1) e2 = ListNode(2) e3 = ListNode(3) e4 = ListNode(4) e5 = ListNode(5) e6 = ListNode(6) e7 = ListNode(7) head = e1 e1.next = e2 e2.next = e3 e3.next = e4 e4.next = e5 e5.next = e6 e6.next = e7 listprint(head) print('-----') listprint(removeNthFromEnd(head,7))