Skip to content

Instantly share code, notes, and snippets.

@MrSmith33
Created May 1, 2020 22:09
Show Gist options
  • Select an option

  • Save MrSmith33/78d4bb82425f18df7ee3de6b8d9e8b0c to your computer and use it in GitHub Desktop.

Select an option

Save MrSmith33/78d4bb82425f18df7ee3de6b8d9e8b0c to your computer and use it in GitHub Desktop.
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