예제 #1
0
    if meeting_node == None:
        return None
    #print(meeting_node.value)

    #计算环中节点个数
    num = CountNode(meeting_node)
    #print(num)

    #快慢指针
    fast = slow = head
    while num > 0:
        fast = fast.next
        num -= 1
    
    while fast != slow:
        fast = fast.next 
        slow = slow.next 
    return slow 


a, b, c, d, e = List.Node(1), List.Node(2), List.Node(3), List.Node(4), List.Node(5)
head = a
head.next = b
head.next.next = c
head.next.next.next = d
head.next.next.next.next = e
head.next.next.next.next.next = c # 3这个节点会出现环

re = EntryNodeOfLoop(head)
print(re.value)
예제 #2
0
파일: 8.py 프로젝트: ChinaChenp/Knowledge
    while cur != None:
        if cur.value <= key:
            if left_list == None:
                left_head = left_list = cur
            else:
                left_list.next = cur
                left_list = cur
        else:
            if right_list == None:
                right_head = right_list = cur
            else:
                right_list.next = cur
                right_list = cur 
        cur = cur.next 

    #拼接左右链表
    right_list.next = None
    left_list.next = right_head 
    return left_head

l2 = List.Node(6)
l2.next = List.Node(1)
l2.next.next = List.Node(4)
l2.next.next.next = List.Node(3)
l2.next.next.next.next = List.Node(5)
l2.next.next.next.next.next = List.Node(1)

re=PartitionList(l2, 3)
List.Print(re)

예제 #3
0
파일: 5.py 프로젝트: ChinaChenp/Knowledge
import list_common as List


def FindKthToTail(head, n):
    if head == None or n == 0:
        return None

    # 快指针先走N步
    fast = slow = head
    for _ in range(0, n):
        if fast != None:
            fast = fast.next
        else:
            return None  # 异常情况链表不够长

    # 快慢指针同时走
    while fast:
        fast = fast.next
        slow = slow.next
    return slow.value


a, b, c, d = List.Node(1), List.Node(2), List.Node(3), List.Node(4)
head = a
head.next = b
head.next.next = c
head.next.next.next = d
print(FindKthToTail(head, 1))
print(FindKthToTail(head, 3))
예제 #4
0
파일: 1.py 프로젝트: ChinaChenp/Knowledge
    elif diff < 0:
        for _ in range(0, diff):
            l2 = l2.next
    #else 一样长

    #同时遍历
    while l1 != None and l2 != None:
        if l1 == l2:
            return l1
        
        l1 = l1.next
        l2 = l2.next 
    return None


a, b, c, d, e, f, g, h = List.Node(1), List.Node(2), List.Node(
    3), List.Node(4), List.Node(5), List.Node(6), List.Node(7), List.Node(8)
l2 = a
l2.next = c
l2.next.next = e
l2.next.next.next = g
l2.next.next.next.next = h

l1 = b
l1.next = d
l1.next.next = f
l1.next.next.next = g
l1.next.next.next.next = h

re = FindCommonNode(l1, l2)
print(re.value)
예제 #5
0
파일: 9.py 프로젝트: ChinaChenp/Knowledge
    if head == None:
        return None

    cur = head
    while cur != None:
        pre = cur
        next_node = cur.next

        # 相同节点都是连续的,从当前节点往后找相同节点把相同节点删掉
        while next_node != None:
            if cur.value == next_node.value:
                pre.next = next_node.next  #删除相同节点
            else:
                pre = next_node  #调整节点
            next_node = next_node.next
        cur = cur.next
    return head


l2 = List.Node(1)
l2.next = List.Node(2)
l2.next.next = List.Node(3)
l2.next.next.next = List.Node(3)
l2.next.next.next.next = List.Node(4)
l2.next.next.next.next.next = List.Node(4)
l2.next.next.next.next.next.next = List.Node(9)
l2.next.next.next.next.next.next.next = List.Node(5)
l2.next.next.next.next.next.next.next.next = List.Node(5)

re = delSameKey(l2)
List.Print(re)
예제 #6
0
        if l1.value < l2.value:
            cur.next = l1
            l1 = l1.next
        else:
            cur.next = l2
            l2 = l2.next
        cur = cur.next # 当前游标指针往后移动
        List.Print(head)
    
    if l1 != None:
        cur.next = l1
    if l2 != None:
        cur.next = l2
    return head

a, b, c, d, e = List.Node(1), List.Node(3), List.Node(7), List.Node(9), List.Node(10)
head1 = a
head1.next = b
head1.next.next = c
head1.next.next.next = d
head1.next.next.next.next = e

a1, b1, c1, d1, e1 = List.Node(0), List.Node(3), List.Node(3), List.Node(8), List.Node(11)
head2 = a1
head2.next = b1
head2.next.next = c1
head2.next.next.next = d1
head2.next.next.next.next = e1
head2.next.next.next.next.next = List.Node(12)

#re = MergeSortList(head1, head2)