def reverse_mn(node, m, n): # 6 # 给一个单链表和正整数 m, n(m < n), 从 m 到 n 开始反转 mNode = linkedlist1.kth_node(node, m) nNode = linkedlist1.kth_node(node, n) l1 = LinkedList() l2 = LinkedList() l1.head.next = mNode.next l2.head.next = nNode.next nNode.next = None l1 = linkedlist1.reverse(l1) mNode.next = l1.head.next return linkedlist1.merge_list(node, l2)
def test_insert_after(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') linkedlist1.append(List, '3') linkedlist1.append(List, '5') linkedlist1.insert_after(List, 1, '2') value = linkedlist1.kth_node(List, 2).value s = 'test_insert_after failed ({})'.format(value) assert str(2) == str(value), s linkedlist1.insert_after(List, 3, '4') value = linkedlist1.kth_node(List, 4).value s = 'test_insert_after2 failed ({})'.format(value) assert str(4) == str(value), s
def reverse_list_k(node, k): # 10 # k 个一组反转链表(25) # 给一个链表, 以每 k 个为一组来翻转链表 # 例子: # Given this linked list: 1->2->3->4->5 # # k = 2, return: 2->1->4->3->5 # # k = 3, return: 3->2->1->4->5 ln = linkedlist1.kth_node(node, k) l = LinkedList() l._length = k n = node.head.next l.head.next = n node.head.next = ln.next ln.next = None l = linkedlist1.reverse(l) ln = linkedlist1.last_node(l) ln.next = node.head.next return l
def sort_list(node): # 5 # 给一个链表, 将链表排序 # 要求: 时间复杂度为 O(n log n) l = linkedlist1.length(node) if l <= 1: return node pass pivotIndex = math.floor(l / 2) pivot = linkedlist1.kth_node(node, pivotIndex) left = LinkedList() #xiao right = LinkedList() #da h = node.head n = h.next while n != None: h.next = n.next if n.value > pivot.value: prependNode(right, n) else: prependNode(left, n) pass n = h.next pass #递归排序2个链表 left = sort_list(left) right = sort_list(right) #合并2个链表 nn = linkedlist1.merge_list(left, right) return nn
def reorder(node): # 3 # 给一个链表, 将原链表 L0 -> L1 -> L2 -> ... -> Ln-1 -> ln 排序为 # L0 -> L1 -> Ln -> L2 -> Ln-1 -> ... # 要求: 不能修改节点的值 l = linkedlist1.length(node) if (l % 2) == 0: l -= 1 pass count = math.floor(l / 2) #插入次数 # 1 3, 2 5, 3 7, 4 8 index = 2 for i in range(count): #得到最后一个node, 删除他 sn = linkedlist1.n_last(node, 2) #返回倒数第二个 ln = sn.next #最后一个node #插入到 node 【index】 后面 iNode = linkedlist1.kth_node(node, index) ln.next = iNode.next iNode.next = ln index += 2 pass return node
def test_kth_node(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') linkedlist1.append(List, '2') linkedlist1.append(List, '3') value = linkedlist1.kth_node(List, 1).value s = 'test_last_node1 failed ({})'.format(value) assert str(1) == str(value), s value = linkedlist1.kth_node(List, 2).value s = 'test_last_node2 failed ({})'.format(value) assert str(2) == str(value), s value = linkedlist1.kth_node(List, 3).value s = 'test_last_node3 failed ({})'.format(value) assert str(3) == str(value), s
def test_prepend(): List = linkedlist1.LinkedList() linkedlist1.prepend(List, '1') linkedlist1.prepend(List, '2') linkedlist1.prepend(List, '3') value = linkedlist1.kth_node(List, 1).value s = 'test_test_prepend1 failed ({})'.format(value) assert str(3) == str(value), s value = linkedlist1.kth_node(List, 2).value s = 'test_test_prepend2 failed ({})'.format(value) assert str(2) == str(value), s value = linkedlist1.kth_node(List, 3).value s = 'test_test_prepend3 failed ({})'.format(value) assert str(1) == str(value), s
def test_delete_last_n(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') linkedlist1.append(List, '3') linkedlist1.append(List, '4') linkedlist1.delete_last_n(List, 2) value = linkedlist1.kth_node(List, 2).value s = 'test_delete_last_n failed ({})'.format(value) assert str(4) == str(value), s
def circle_head(node): # 2 # 给一个链表, 返回环形链表中环形开始的节点, 如果没有环形, 返回 None # <1> 定义两个指针p1和p2,在初始化时都指向链表的头节点。 # <2> 如果链表中的环有n个节点,指针p1先在链表上向前移动n步。 # <3> 然后指针p1和p2以相同的速度在链表上向前移动直到它们相遇。 # <4> 它们相遇的节点就是环的入口节点。 # 那么如何得到环中的节点数目?可使用上述方法(1),即通过一快一慢两个指针来解决这个问题。当两个指针相遇时,表明链表中存在环。 # 两个指针相遇的节点一定是在环中。可以从这个节点出发,一边继续向前移动一边计数,当再次回到这个节点时,即可得到环中的节点数了。 #1. 判断是不是环形 p1 = node.head.next p2 = p1.next jd = {} # p1和p2的交点 while p2.next != None: v1 = p1.value v2 = p2.value if v1 == v2: jd = p1 break pass p1 = p1.next p2 = p2.next.next pass if jd == {}: return None pass hSize = 1 #环的长度 n = jd.next while jd.value != n.value: hSize += 1 n = n.next pass hNode = {} #环开始节点\ p1 = linkedlist1.kth_node(node, hSize + 1) p2 = node.head.next while hSize > 0: p1 = p1.next p2 = p2.next if p1.value == p2.value: hNode = p1 pass hSize -= 1 pass return hNode
def test_power_copye(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') linkedlist1.append(List, '2') linkedlist1.append(List, '3') linkedlist1.append(List, '4') linkedlist1.append(List, '5') linkedlist1.append(List, '6') linkedlist1.append(List, '7') linkedlist1.append(List, '8') node8 = linkedlist1.last_node(List) node5 = linkedlist1.kth_node(List, 5) node8.next = node5 # 6 # 7 5 # 1 2 3 4 8 cList = linkedlist1.power_copy(List) value = linkedlist1.kth_node(List, 11).value s = 'test_has_x1 failed ({})'.format(value) assert str(7) == str(value), s
def rotate_list(node, k): # 4 # 给一个链表, 将列表向右旋转 k 个下标, 其中 k 是非负数 # 例子: # Input: 1->2->3->4->5->NULL, k = 2 # Output: 4->5->1->2->3->NULL # Input: 0->1->2->NULL, k = 4 # Output: 2->0->1->NULL l = linkedlist1.length(node) if l < k: k = k % l pass i = l - k iNode = linkedlist1.kth_node(node, i) lNode = linkedlist1.last_node(node) h = node.head lNode.next = h.next h.next = iNode.next iNode.next = None return node
def test_circle_head(): List = linkedlist2.LinkedList() linkedlist2.append(List, '1') linkedlist2.append(List, '2') linkedlist2.append(List, '3') linkedlist2.append(List, '4') linkedlist2.append(List, '5') linkedlist2.append(List, '6') linkedlist2.append(List, '7') linkedlist2.append(List, '8') a = linkedlist2.circle_head(List) s = 'test_length1 failed ({})'.format(a) assert str(None) == str(a), s #制造环形链表 node8 = linkedlist1.last_node(List) node5 = linkedlist1.kth_node(List, 5) node8.next = node5 # 6 # 7 5 # 1 2 3 4 8 a = linkedlist2.circle_head(List).value s = 'test_length1 failed ({})'.format(a) assert str(5) == str(a), s