class Solution: def __init__(self, *args): self.single_link_list = SingleLinkList(None, *args) print("初始化: ", end="") self.single_link_list.travel() def reverseKGroup(self, k): start, end, self.next = self, self.single_link_list.head, self.single_link_list.head has = 0 count = 1 while end: # 如果本节点不为空 if count == k: prev, cur = start.next, start.next.next # 从start节点开始 while not (prev is end): cur.next, prev, cur = prev, cur, cur.next start.next.next, start.next, start = cur, prev, start.next if not has: self.next = end has = 1 end = start.next count = 1 else: end = end.next # 指针往下走 count += 1 # 计数 self.single_link_list.travel(self.next)
def __init__(self, pos=-1, *args): single_link_list = SingleLinkList(None, *args) if 0 <= pos <= len(args) - 1: count = 0 cur = single_link_list.head while cur != None: if count == pos: break else: count += 1 cur = cur.next single_link_list.tail.next = cur self.single_link_list = single_link_list
# 通过快慢指针找到链表中点 slow = head fast = head # 每次循环快指针遍历2个结点,慢指针遍历1个结点 while fast and fast.next: slow = slow.next fast = fast.next.next mid = slow # 找到中点结点 pre = None while slow: # 后半段链表反转 slow.next, pre, slow = pre, slow, slow.next # 反转后半段链表的头结点为pre,前半段链表的头结点为head while head != mid: # 同时遍历两半段链表,直到中点; if head.val != pre.val: return False head = head.next pre = pre.next return True if __name__ == '__main__': from single_link_list import SingleLinkList sll = SingleLinkList() for i in range(4): sll.add_rear(i) for i in range(3, -1, -1): sll.add_rear(i) sll.travel() print(isPalindrome(sll.head)) print(isPalindrome2(sll.head))
def __init__(self, *k): link_list = SingleLinkList(None, *k) print("初始化: ", end=" ") link_list.travel() self.link_list = link_list
pre = head head = head.next pre.next = pre_pre return pre def reverse2(head): pre = None while head: head.next, pre, head = pre, head, head.next return pre if __name__ == '__main__': from single_link_list import SingleLinkList ssl = SingleLinkList() for i in range(2): ssl.add_rear(i) ssl.travel() head = reverse_list(ssl.head) ssl2 = SingleLinkList(head) ssl2.travel() ss3 = SingleLinkList() for i in range(5): ss3.add_rear(i) ss3.travel() head = reverse2(ss3.head) ss4 = SingleLinkList(head) ss4.travel()
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: dummy = ListNode(0) dummy.next = head slow = fast = dummy while n: fast = fast.next n -= 1 while fast.next: slow = slow.next fast = fast.next slow.next = slow.next.next return dummy.next if __name__ == '__main__': sll = SingleLinkList() for i in range(2): sll.add_rear(i + 1) new_sll = SingleLinkList(removeNthFromEnd(sll.head, 1)) new_sll.travel() sll2 = SingleLinkList() for i in range(2): sll2.add_rear(i + 1) ns = SingleLinkList(remove2(sll2.head, 2)) ns.travel() sll3 = SingleLinkList() for i in range(2): sll3.add_rear(i + 1) ns = SingleLinkList(remove3(sll3.head, 1))
head.next = ListNode(sum_val) head = head.next l1 = l1.next while l2: sum_val = l2.val + flag if sum_val >= 10: sum_val %= 10 flag = 1 else: flag = 0 head.next = ListNode(sum_val) head = head.next l2 = l2.next if flag: head.next = ListNode(1) return dummy.next if __name__ == '__main__': from single_link_list import SingleLinkList s1 = SingleLinkList() s2 = SingleLinkList() for i in range(1, 2): s1.add_rear(i) s2.add_rear(9) s2.add_rear(9) # s = SingleLinkList(addTwoNumbers(s1.head, s2.head)) s = SingleLinkList(addt(s1.head, s2.head)) s.travel()
def __init__(self, *args): self.single_link_list = SingleLinkList(None, *args) print("初始化: ", end="") self.single_link_list.travel()