<jupyter_start><jupyter_text>Title : Linked Lists의 합 구하기Chapter : Linked ListLink :ChapterLink :문제 : 주어진 LinkedLists의 역방향 합과 정방향 합을 구하여라list1 : 7→5→3list2 : 4→8→2역방향답 : 1→4→6정방향답 :  1→2→3→5<jupyter_code>from typing import List

class ListNode:
  def __init__(self, x):
    self.val = x
    self.next = None

def createList(in_list:List[int]) -> ListNode:
  if len(in_list) == 0:
    raise RuntimeError("in_list must have data")        
  head_node = ListNode(in_list[0])
  last_node = head_node
  for idx in range(1,len(in_list)):
    node = ListNode(in_list[idx])
    last_node.next = node
    last_node = node
  return head_node

def printNodes(node:ListNode):
  crnt_node = node
  while crnt_node is not None:
    print(crnt_node.val, end=' ')
    crnt_node = crnt_node.next
  print()
class ListAdder:
  def addReverse(self, l1: ListNode, l2: ListNode) -> ListNode:
    dummy_node = ListNode(-1)
    crnt_node = dummy_node
    node_a = l1
    node_b = l2
    carry = 0
    
    while node_a or node_b:
      num_a = node_a.val if node_a else 0
      num_b = node_b.val if node_b else 0
      sum_ab = num_a + num_b + carry
      carry = int(sum_ab / 10)
      val = int(sum_ab % 10)
      
      new_node = ListNode(val)
      crnt_node.next = new_node
      crnt_node = crnt_node.next
      node_a = node_a.next if node_a else None
      node_b = node_b.next if node_b else None   
    
    if 1 == carry:
      new_node = ListNode(carry)
      crnt_node.next = new_node

    return dummy_node.next

  def addForward(self, l1: ListNode, l2: ListNode) -> ListNode:
    reverse_head1 = self.__reverseList(l1)
    reverse_head2 = self.__reverseList(l2)

    reverse_sum = self.addReverse(reverse_head1,reverse_head2)
    forward_sum = self.__reverseList(reverse_sum)
    return forward_sum

  def __reverseList(self, head: ListNode) -> ListNode:
    if head is None:
      return head
    elif head.next is None:
      return head    
    
    crnt_node = head.next
    prev_node = head
    head.next = None
    
    while crnt_node:
      tmp_next_node = crnt_node.next
      crnt_node.next = prev_node
      prev_node = crnt_node
      crnt_node = tmp_next_node
      
    return prev_node

adder = ListAdder()
list1 = createList([7,5,3])
list2 = createList([4,8,2])
reverseAdd = adder.addReverse(list1,list2)
printNodes(reverseAdd)
list1 = createList([7,5,3])
list2 = createList([4,8,2])
reverseAdd = adder.addForward(list1,list2)
printNodes(reverseAdd)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09569762766385204, 'fraction_numerical': 0.02372336147969441, 'mean_word_length': 3.098846787479407, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '463266', 'license': 'MIT', 'n_tokens_mistral': 973, 'n_tokens_neox': 985, 'n_words': 213, 'path': 'linkedList/addLists.ipynb', 'repo_name': 'NoCodeProgram/CodingTest', 'size': '2487'}