def rearrange(node, x): # 1 # 给一个单链表和一个值 x, 对它进行分割, 使得所有小于 x 的节点都在节点大于或等于 x 之前, # 新建俩个链表,一个存放小于x的节点,另一个存放大于等于x的节点。最后将链表和在一起。就得到结果 l1 = LinkedList() #大于x的链表 l2 = LinkedList() h = node.head n = node.head.next while h.next != None: h.next = n.next v = n.value if v >= x: prependNode(l1, n) else: prependNode(l2, n) pass n = h.next pass l1._length += l2._length ln = linkedlist1.last_node(l2) ln.next = l1.head.next l1.head.next = l2.head.next return l1
def test_last_node(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') value = linkedlist1.last_node(List).value s = 'test_last_node1 failed ({})'.format(value) assert str(1) == str(value), s linkedlist1.append(List, '2') value = linkedlist1.last_node(List).value s = 'test_last_node2 failed ({})'.format(value) assert str(2) == str(value), s linkedlist1.append(List, '3') value = linkedlist1.last_node(List).value s = 'test_last_node3 failed ({})'.format(value) assert str(3) == 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 test_delete_n(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') linkedlist1.append(List, '3') linkedlist1.append(List, '4') List = linkedlist1.delete_n(List, 3) value = linkedlist1.last_node(List).value s = 'test_delete_n failed ({})'.format(value) assert str(3) == str(value), s
def test_is_circle(): List = linkedlist1.LinkedList() linkedlist1.append(List, '1') linkedlist1.append(List, '2') linkedlist1.append(List, '3') value = linkedlist1.is_circle(List) s = 'test_has_x1 failed ({})'.format(value) assert str(False) == str(value), s node = linkedlist1.last_node(List) node.next = List.head.next value = linkedlist1.is_circle(List) s = 'test_has_x1 failed ({})'.format(value) assert str(True) == str(value), s
def test_joseph_list(): 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') linkedlist1.append(List, '9') lNode = linkedlist1.last_node(List) lNode.next = List.head ######创建环链表 l = linkedlist1.joseph_list(List, 9)
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
def append(node, x): # 7, 给单链表末尾插入一个元素 node._length += 1 lastNode = linkedlist1.last_node(node) lastNode.next = ListNode(x) return node