コード例 #1
0
from helper import Node

def has_cycle(list_head):
  current = list_head
  while current.next != None:
    if current.next == list_head:
      return True
      break
    else:
      current = current.next


  return False
  



firstHead = Node(3)
secondNode = Node(4)
thirdNode = Node(5)

firstHead.next = secondNode
secondNode.next = thirdNode
thirdNode.next = firstHead
print(has_cycle(firstHead))
コード例 #2
0
ファイル: add_tail.py プロジェクト: ronitshar/oop_02
from helper import Node


def add_tail(list_head, val):
    current = list_head
    while current.next != None:
        current = current.next

    current.next = Node(val)


def print_list(list_head):
    current = list_head
    while current != None:
        print(current.content)
        current = current.next


firstHead = Node(3)
secondNode = Node(4)
thirdNode = Node(5)

firstHead.next = secondNode
secondNode.next = thirdNode
add_tail(firstHead, 6)
print_list(firstHead)
コード例 #3
0
        print(current.content)
        current = current.next


def merge(train1, train2):
    current = train1
    while current.next != None:
        current = current.next

    current.next = train2
    return train1


train1Head = Node(5)
train1node2 = Node(4)
train1node3 = Node(3)

train1Head.next = train1node2
train1node2.next = train1node3

train2Head = Node(6)
train2node2 = Node(5)
train2node3 = Node(4)

train2Head.next = train2node2
train2node2.next = train2node3

merged = merge(train1Head, train2Head)
final = sort_asc(merged)

print_list(final)