【模板总结】链表相加 正序和反序

2. 两数相加

在这里插入图片描述

   public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
   
      ListNode head1 = l1;
      ListNode head2 = l2;
      int carry = 0;
      ListNode dummy = new ListNode(-1);
      ListNode cur = dummy;
      while(head1 != null || head2 != null){
   
         int x = head1 == null ? 0 : head1.val;
         int y 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个用 Python 实现的二叉搜索树,并将其转换成双向链表,以及分别按正序反序输出双向链表中的所有元素的代码示例: ```python class Node: def __init__(self, value=None): self.value = value self.left_child = None self.right_child = None class BinarySearchTree: def __init__(self): self.root = None def insert(self, value): if self.root is None: self.root = Node(value) else: self._insert(value, self.root) def _insert(self, value, current_node): if value < current_node.value: if current_node.left_child is None: current_node.left_child = Node(value) else: self._insert(value, current_node.left_child) elif value > current_node.value: if current_node.right_child is None: current_node.right_child = Node(value) else: self._insert(value, current_node.right_child) else: print("Value already exists in tree.") def print_tree(self): if self.root is not None: self._print_tree(self.root) def _print_tree(self, current_node): if current_node is not None: self._print_tree(current_node.left_child) print(str(current_node.value)) self._print_tree(current_node.right_child) def convert_to_dll(self): if self.root is not None: self._convert_to_dll(self.root) def _convert_to_dll(self, current_node): if current_node.left_child is not None: left = self._convert_to_dll(current_node.left_child) while left.right_child is not None: left = left.right_child left.right_child = current_node current_node.left_child = left if current_node.right_child is not None: right = self._convert_to_dll(current_node.right_child) while right.left_child is not None: right = right.left_child right.left_child = current_node current_node.right_child = right return current_node def print_dll(self): if self.root is not None: current_node = self.root while current_node.left_child is not None: current_node = current_node.left_child while current_node is not None: print(str(current_node.value)) current_node = current_node.right_child def print_reverse_dll(self): if self.root is not None: current_node = self.root while current_node.right_child is not None: current_node = current_node.right_child while current_node is not None: print(str(current_node.value)) current_node = current_node.left_child ``` 使用方法: ```python bst = BinarySearchTree() bst.insert(5) bst.insert(3) bst.insert(7) bst.insert(1) bst.insert(9) print("Printing binary search tree:") bst.print_tree() bst.convert_to_dll() print("Printing doubly linked list in forward order:") bst.print_dll() print("Printing doubly linked list in reverse order:") bst.print_reverse_dll() ``` 输出结果: ``` Printing binary search tree: 1 3 5 7 9 Printing doubly linked list in forward order: 1 3 5 7 9 Printing doubly linked list in reverse order: 9 7 5 3 1 ``` 该代码示例中,我们创建了一个 `Node` 类表示二叉搜索树的节点,以及一个 `BinarySearchTree` 类表示二叉搜索树。`BinarySearchTree` 类包含了插入节点、遍历二叉搜索树、将二叉搜索树转换成双向链表,以及分别按正序反序输出双向链表中的所有元素的方法。我们使用 `insert` 方法将新节点插入二叉搜索树中,使用 `print_tree` 方法遍历打印二叉搜索树的节点值。使用 `convert_to_dll` 方法将二叉搜索树转换成双向链表。使用 `print_dll` 方法按正序输出双向链表中的所有元素,使用 `print_reverse_dll` 方法按反序输出双向链表中的所有元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值