Skip to content

Instantly share code, notes, and snippets.

@ariok
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariok/e1aa44ee440784f484d9 to your computer and use it in GitHub Desktop.
Save ariok/e1aa44ee440784f484d9 to your computer and use it in GitHub Desktop.
// Playground - noun: a place where people can play
import Cocoa
/* Node class implementation */
class Node
{
var data:String?
var link:Node?
init(data:String?, link:Node? = nil)
{
self.data = data
self.link = link
}
}
/* Helper functions */
func printList(node:Node?)
{
var l:Node? = node
println("\nPrinting --------");
while l{
println("\(l!.data)")
l = l!.link
}
}
func reverseList(var node:Node?)->(Node?)
{
var new_root:Node?
while node{
var next = node!.link
node!.link = new_root
new_root = node
node = next
}
return new_root
}
/* Setup the list */
var d = Node(data:"d");
var c = Node(data:"c", link:d);
var b = Node(data:"b", link:c);
var a = Node(data:"a", link:b);
/* Print and reverse */
printList(a);
var r = reverseList(a);
printList(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment