def solution(myLinked): list = [] size = len(myLinked) nowLinked = myLinked.head if size % 2 == 1: for i in range(int(size / 2)): list.append(nowLinked.value) nowLinked = nowLinked.next nowLinked = nowLinked.next else: for i in range(size / 2): list.append(nowLinked.value) nowLinked = nowLinked.next flag = True for i in range(1, int(size / 2) + 1): reverse = i * -1 if (list[(-1) * i] != nowLinked.value): flag = False nowLinked = nowLinked.next return flag ll_true = LinkedList([1, 2, 3, 4, 5, 4, 3, 2, 1]) print(solution(ll_true)) ll_false = LinkedList([1, 2, 3, 4, 5, 6, 7, 8, 9]) print(solution(ll_false))
def check10(longllData): if longllData.value > 10: longllData.next.value = longllData.next.value + 1 longllData.value = longllData.value - 10 def solution(ll1, ll2): longll, shortll = ll1 if len(ll1) >= len(ll2) else ll2, ll1 if len( ll1) < len(ll2) else ll2 nowLong = longll.head nowShort = shortll.head for i in range(len(shortll)): nowLong.value = nowLong.value + nowShort.value check10(nowLong) nowLong = nowLong.next nowShort = nowShort.next check10(nowLong) ll_a = LinkedList() ll_a.generate(2, 0, 9) ll_b = LinkedList() ll_b.generate(3, 0, 9) solution(ll_a, ll_b) print(ll_b)
from Chap2.LinkedList import LinkedList def partition(ll, key): befLL = LinkedList() aftLL = LinkedList() keyLL = LinkedList() now = ll.head for _ in range(len(ll)): if now.value > key: aftLL.add(now.value) elif now.value == key: keyLL.add(now.value) elif now.value < key: befLL.add(now.value) now = now.next befLL.add_multiple(keyLL) befLL.add_multiple(aftLL) print(befLL) ll = LinkedList() ll.add_multiple([3, 5, 8, 5, 10, 2, 1]) key = 5 partition(ll, key)
def partition(ll, key): befLL = LinkedList() aftLL = LinkedList() keyLL = LinkedList() now = ll.head for _ in range(len(ll)): if now.value > key: aftLL.add(now.value) elif now.value == key: keyLL.add(now.value) elif now.value < key: befLL.add(now.value) now = now.next befLL.add_multiple(keyLL) befLL.add_multiple(aftLL) print(befLL)
from Chap2.LinkedList import LinkedList def deleteMiddle(node): #두 문장의 순서도 중요하다, 값 먼저 바꾸고 next 변경 node.value = node.next.value node.next = node.next.next ll = LinkedList() ll.add_multiple([1, 2, 3, 4]) middle_node = ll.add(5) ll.add_multiple([7, 8, 9]) print(ll) deleteMiddle(middle_node) print(ll) #헤드 접근 없이 삭제할 노드에만 접근이 가능하다는 것 / 나는 c에는 접근할 수 있다.