Exemplo n.º 1
0
        if meeting_node == start_node:
            break

    first = l.head
    later = l.head
    for _ in range(count):
        first = first.next
    while True:
        if first == later:
            return later.data
        else:
            later = later.next
            first = first.next


if __name__ == '__main__':

    # 初始化一个带环的链表
    l = LinkList()
    l.init_list([1, 3, 4, 5, 6, 9])
    head = l.head
    target_node = head.next.next

    for _ in range(l.get_length()):
        if not head.next:
            head.next = target_node
        else:
            head = head.next

    result = find_entrance(l)
    print(result)
Exemplo n.º 2
0
    定义两个指针 先让第一个指针走k-1步 使两个指针保持k的距离
    然后两个指针一起走 直到第一个指针走到链表末尾 第二个指针此时所指向的位置即为倒数第k个节点位置"""

from List.linklist_base import LinkList


def find_kth_node_from_last(link_list, k):

    if link_list.is_empty() or k == 0:
        return None
    p_head = link_list.head
    p_behind = link_list.head

    for i in range(k - 1):
        if p_head.next:
            p_head = p_head.next
        else:
            return None
    while p_head.next is not None:
        p_behind = p_behind.next
        p_head = p_head.next

    return p_behind


if __name__ == '__main__':

    l = LinkList()
    l.init_list([1, 5, 8, 9, 23, 4, 10])
    result = find_kth_node_from_last(l, 3)
    print(result.data)
Exemplo n.º 3
0
    len_dif = len1 - len2
    if len_dif < 0:
        len_dif = len2 - len1
        for _ in range(len_dif):
            l2_head = l2_head.next
        for _ in range(len1):
            if l1_head.data == l2_head.data:
                return l1_head.data
            else:
                l1_head = l1_head.next
                l2_head = l2_head.next
    else:
        for _ in range(len_dif):
            l1_head = l1_head.next

        for _ in range(len2):
            if l1_head.data == l2_head.data:
                return l1_head.data
            else:
                l1_head = l1_head.next
                l2_head = l2_head.next


if __name__ == '__main__':

    l1 = LinkList()
    l2 = LinkList()
    l1.init_list([1, 9, 7, 10, 3, 4])
    l2.init_list([2, 6, 10, 3, 4])
    result = find_public_node(l1, l2)
    print(result)
Exemplo n.º 4
0
    遍历时 首先临时存储当前节点的后一节点 然后把当前节点的后一节点指向前一节点、前一节点=当前节点、当前节点=后一节点"""

from List.linklist_base import LinkList


def list_reverse(link_list):

    if not link_list.head:
        return link_list

    p_head = link_list.head
    p_last = None

    while p_head:
        tmp = p_head.next
        p_head.next = p_last
        p_last = p_head
        p_head = tmp

    return p_last


if __name__ == '__main__':

    l = LinkList()
    l.init_list([1, 3, 6, 5, 8])
    head = list_reverse(l)
    while head:
        print(head.data)
        head = head.next
Exemplo n.º 5
0
        if cur_node.next and cur_node.data == cur_node.next.data:
            next_node = cur_node.next
            # 找到下一个不等的节点
            while next_node.next and next_node.next.data == cur_node.data:
                next_node = next_node.next

            # 前两个节点已删除 故head_node指向第三个节点或者更后面的节点
            if cur_node == head_node:
                # 更新头节点
                head_node = next_node.next
            else:
                pre_node.next = next_node.next

            cur_node = next_node.next
        else:
            # 当前节点与next节点不等
            pre_node = cur_node
            cur_node = cur_node.next

    return head_node


if __name__ == '__main__':

    l = LinkList()
    l.init_list([1, 2, 3, 3, 4, 4, 5])

    result = delete_repeated_node(l)
    while result:
        print(result.data)
        result = result.next
Exemplo n.º 6
0
# -*- coding:utf-8 -*-


""" 从尾到头打印链表
    通常,这种情况下,我们不希望修改原链表的结构。
    返回一个反序的链表,这就是经典的“后进先出”,我们可以使用栈实现这种顺序。
    每经过一个结点的时候,把该结点放到一个栈中。当遍历完整个链表后,再从栈顶开始逐个输出结点的值,
    给一个新的链表结构,这样链表就实现了反转。"""

""" 对于python,可以直接使用列表的插入方法,每次插入数据只插入在首位"""


from List.linklist_base import LinkList


def print_list_tail2head(LinkList):

    result = []
    p = LinkList.head
    while p:
        result.insert(0, p.data)
        p = p.next
    return result


if __name__ == '__main__':

    node = LinkList()
    node.init_list([1, 4, 7, 9])
    print(print_list_tail2head(node))
Exemplo n.º 7
0
        tmp = head
        count = k
        while tmp and count:
            the_stack.append(tmp)
            tmp = tmp.next
            count -= 1

        if count:
            p.next = head
            break

        while the_stack:
            p.next = the_stack.pop()
            p = p.next

        p.next = tmp
        head = tmp

    return start_node.next


if __name__ == '__main__':

    the_list = LinkList()
    the_list.init_list([1, 2, 3, 4, 5])
    the_head = the_list.head
    result = reverse_k_group(the_head, 2)
    while result:
        print(result.data)
        result = result.next