Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 145
Good morning! Here's your coding interview problem for today.

This problem was asked by Google.

Given the head of a singly linked list, swap every two nodes and return its head.

For example, given `1 -> 2 -> 3 -> 4`, return `2 -> 1 -> 4 -> 3`.

Pull request description:
Day145
Resolves #302.

Patch:
diff --git a/README.md b/README.md
index bb128c6..a54e76f 100644
--- a/README.md
+++ b/README.md
@@ -154,5 +154,6 @@ problems from
 * [Day 141](https://github.com/vaskoz/dailycodingproblem-go/issues/294)
 * [Day 142](https://github.com/vaskoz/dailycodingproblem-go/issues/297)
 * [Day 143](https://github.com/vaskoz/dailycodingproblem-go/issues/299)
+* [Day 145](https://github.com/vaskoz/dailycodingproblem-go/issues/302)
 * [Day 146](https://github.com/vaskoz/dailycodingproblem-go/issues/303)
 * [Day 147](https://github.com/vaskoz/dailycodingproblem-go/issues/304)
diff --git a/day145/problem.go b/day145/problem.go
new file mode 100644
index 0000000..4c95f8b
--- /dev/null
+++ b/day145/problem.go
@@ -0,0 +1,18 @@
+package day145
+
+// LL is a singly linked list of integers.
+type LL struct {
+	Value int
+	Next  *LL
+}
+
+// SwapEveryTwo swaps every two nodes in a linked list.
+func SwapEveryTwo(head *LL) *LL {
+	if head == nil || head.Next == nil {
+		return head
+	}
+	result := head.Next
+	result.Next, head.Next = head, head.Next.Next
+	result.Next.Next = SwapEveryTwo(result.Next.Next)
+	return result
+}


Test patch:
diff --git a/day145/problem_test.go b/day145/problem_test.go
new file mode 100644
index 0000000..1c8a776
--- /dev/null
+++ b/day145/problem_test.go
@@ -0,0 +1,42 @@
+package day145
+
+import "testing"
+
+var testcases = []struct {
+	input, expected *LL
+}{
+	{&LL{1, &LL{2, &LL{3, &LL{4, nil}}}},
+		&LL{2, &LL{1, &LL{4, &LL{3, nil}}}},
+	},
+	{&LL{1, &LL{2, &LL{3, &LL{4, &LL{5, nil}}}}},
+		&LL{2, &LL{1, &LL{4, &LL{3, &LL{5, nil}}}}},
+	},
+}
+
+func TestSwapEveryTwo(t *testing.T) {
+	t.Parallel()
+	for tcid, tc := range testcases {
+		if result := SwapEveryTwo(tc.input); !equal(result, tc.expected) {
+			t.Errorf("TCID%d do not match", tcid)
+		}
+	}
+}
+
+func BenchmarkSwapEveryTwo(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			SwapEveryTwo(tc.input)
+		}
+	}
+}
+
+func equal(one, two *LL) bool {
+	for one != nil && two != nil {
+		if one.Value != two.Value {
+			return false
+		}
+		one = one.Next
+		two = two.Next
+	}
+	return one == nil && two == nil
+}