Created
May 1, 2020 22:09
-
-
Save MrSmith33/78d4bb82425f18df7ee3de6b8d9e8b0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std; | |
| struct Node | |
| { | |
| int value; | |
| Node* next; | |
| } | |
| Node* reverseList(Node* head) | |
| { | |
| Node* current = head; | |
| Node* prev; | |
| while (current) | |
| { | |
| Node* next = current.next; | |
| current.next = prev; | |
| prev = current; | |
| current = next; | |
| } | |
| return prev; | |
| } | |
| void printList(Node* node) | |
| { | |
| write("["); | |
| while (node) | |
| { | |
| write(node.value, " "); | |
| node = node.next; | |
| } | |
| writeln("]"); | |
| } | |
| Node* makeList(int[] values) | |
| { | |
| Node* next; | |
| foreach_reverse (int val; values) | |
| next = new Node(val, next); | |
| return next; | |
| } | |
| void main() | |
| { | |
| void test(int[] values) | |
| { | |
| values | |
| .makeList | |
| .reverseList | |
| .printList; | |
| } | |
| test([]); | |
| test([1]); | |
| test([1, 2]); | |
| test([1, 2, 3]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment