Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
List add v2
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Title of this code #include <iostream> using namespace std; struct node { int num; node* next; node(int num, node* next) { this->num = num; this->next = next; } }; int node_to_num(node *n) { int num = n->num; node *cur = n->next; while (cur) { num = (num * 10) + cur->num; cur = cur->next; } return (int)num; } node* num_to_node(int num) { node *n = NULL; while (num > 0) { n = new node(num % 10, n); num /= 10; } return n; } node* node_add(node* n1, node* n2) { int num1 = node_to_num(n1); int num2 = node_to_num(n2); return num_to_node(num1 + num2); } // [1] -> [2] -> [3] -> [4] -> [] // [] <- [1] <- [2] <- [3] <- [4] node* reverse(node* n) { node *perv = NULL; node *cur = n; node *next = n->next; if (next) while (cur) { cur->next = perv; perv = cur; if (next) { cur = next; next = cur->next; } else { break; } } return cur; } void print(node* n) { while(n) { cout << n->num << " "; n = n->next; } cout << endl; } int main() { node *n1 = new node(1, new node(2, new node(3, NULL))); node *n2 = new node(2, new node(3, new node(4, NULL))); n1 = reverse(n1); print(n1); print(n2); node *n = node_add(n1, n2); cout << node_to_num(n); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.32 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.47 sec
fork mode
|
history
|
discussion
3 2 1 2 3 4 555