예제 #1
0
def find_lask_k(head, k):
    if head is None or head.next is None:
        return head
    slow = LNode()
    fast = LNode()
    slow = head.next
    fast = head.next
    i = 0
    while i < k and fast is not None:
        fast = fast.next
        i += 1
    if i < k:  #不够长
        return None
    while fast is not None:
        slow = slow.next
        fast = fast.next
    return slow
예제 #2
0
def my_has_loop(head)->bool:
    #破坏型判断是否有环
    if head is None or head.next is None:
        return False
    cur = head.next
    flag = LNode(0)
    while cur:
        if cur.next is flag:
            return True
        temp = cur.next
        cur.next = flag
        cur = temp
    return False
예제 #3
0
def ConstructList():
    i = 1
    head = LNode()
    head.next = None
    tmp = None
    cur = head
    # 构造第一个链表
    while i < 8:
        tmp = LNode()
        tmp.data = i
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i += 1
    return head
예제 #4
0
    cur=next

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

    cur.next = pre  # 最后一个节点指向倒数第二个节点
    head.next = cur


if __name__ == '__main__':
    i=1
    head=LNode(None)
    head.next=None
    tmp=None
    cur=head
    while i<8:
        tmp=LNode(i)
        tmp.next=None
        cur.next=tmp
        cur=tmp
        i+=1
    print("逆序前:")
    cur=head.next
    while cur is not None:
        print(cur.data)
        cur=cur.next
예제 #5
0
def add(h1, h2):
    if h1 is None or h1.next is None:
        return h2
    if h2 is None or h2.next is None:
        return h1

    c=0#记录进位
    sums=0#记录两个节点相加的值

    p1=h1.next
    p2=h2.next

    tmp=None#存储相加的和
    resultHead=LNode()#相加后的链表的头节点
    resultHead.next=None

    p=resultHead#用来指向链表resultHead最后一个节点
    while p1 is not None and p2 is not None:
        tmp=LNode()
        sums=p1.data + p2.data + c
        tmp.data=sums % 10
        c=int(sums/10)

        p.next = tmp#加入新链表
        p=tmp

        p1=p1.next
        p2=p2.next
    #p2比p1长,p1比p2长
    if p1 is None:
        while p2 is not None:
            tmp=LNode()
            sums=p2.data+c
            tmp.data=sums%10
            c=int(sums/10)
            p.next=tmp
            p=tmp
            p2=p2.next
    if p2 is None:
        while p1 is not None:
            tmp=LNode()
            sums=p1.data+c
            tmp.data=sums%10
            c=int(sums/10)
            p.next=tmp
            p=tmp
            p1=p1.next
    #完成全部计算后还有进位
    if c==1:
        tmp=LNode()
        tmp.data=1
        p.next=tmp
    return resultHead
예제 #6
0
            sums=p1.data+c
            tmp.data=sums%10
            c=int(sums/10)
            p.next=tmp
            p=tmp
            p1=p1.next
    #完成全部计算后还有进位
    if c==1:
        tmp=LNode()
        tmp.data=1
        p.next=tmp
    return resultHead

if __name__ == '__main__':
    i = 1
    head1 = LNode()
    head1.next = None
    head2 = LNode()
    head2.next = None
    tmp = None
    cur = head1
    addResult = None
    # 构造第一个链表
    while i < 7:
        tmp = LNode()
        tmp.data = i + 2
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i += 1
    cur = head2
예제 #7
0
    inner_pre = None
    while out_cur is not None:
        inner_cur = out_cur.next
        inner_pre = out_cur
        while inner_cur is not None:
            if inner_cur.data == out_cur.data:
                inner_pre.next = inner_cur.next
            else:
                inner_pre = inner_cur
            inner_cur = inner_cur.next
        out_cur = out_cur.next


if __name__ == '__main__':
    i = 1
    head = LNode()
    tmp = None
    cur = head
    while i < 7:
        tmp = LNode()
        if i % 2 == 0:
            tmp.data = i + 1
        elif i % 3 == 0:
            tmp.data = i - 2
        else:
            tmp.data = i
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i += 1
    print("删除前:")
예제 #8
0
            else:
                return  #剩余节点数小于k
        end_next = end.next
        end.next = None  #断开链接逆序k个元素内部
        pre.next = reverse(begin)
        begin.next = end_next
        #逆序完成后,回到上次相同的指针状态
        pre = begin
        begin = begin.next
        i = 1


if __name__ == "__main__":
    i = 1

    head = LNode()

    head.next = None
    tmp = None
    cur = head
    while i < 8:
        tmp = LNode()
        tmp.data = i
        tmp.next = None
        cur.next = tmp
        cur = tmp
        i += 1
    print("顺序输出:", end='')
    cur = head.next
    while cur != None:
        print(cur.data, end=' ')

def remove_node(p):
    if p.next is None or p is None:
        return False  #无法删除
    p_next = p.next
    next = p.next.next
    p.data = p_next.data
    p.next = next
    p_next.next = None
    return True


if __name__ == "__main__":
    i = 1
    head = LNode()  # 链表头结点
    head.next = None
    tmp = None
    cur = head
    p = None
    # 构造链表
    while i < 8:
        tmp = LNode()
        tmp.data = i
        tmp.next = None
        cur.next = tmp
        cur = tmp
        if i == 5:
            p = tmp
        i += 1
    print("删除结点" + str(p.data) + "前链表: ")