Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Node Parse(string nodes)
- {
- Node head = null;
- List<string> StringNumbers = nodes.Split(' ').ToList();
- for(int i=0;i<StringNumbers.Count();i++)
- {
- if(int.TryParse(StringNumbers[i], out int s)==true)
- {
- head = new Node(s, head.Next);
- }
- }
- return head;
- }
- // Assert.AreEqual(new Node(1, new Node(2, new Node(3))), Kata.Parse("1 -> 2 -> 3 -> null"));
- //Assert.AreEqual(new Node(0, new Node(1, new Node(4, new Node(9, new Node(16))))), Kata.Parse("0 -> 1 -> 4 -> 9 -> 16 -> null"));
- //Assert.AreEqual(null, Kata.Parse("null"));
- public class Node : Object
- {
- public int Data;
- public Node Next;
- public Node(int data, Node next = null)
- {
- this.Data = data;
- this.Next = next;
- }
- public override bool Equals(Object obj)
- {
- // Check for null values and compare run-time types.
- if (obj == null || GetType() != obj.GetType()) { return false; }
- return this.ToString() == obj.ToString();
- }
- public override string ToString()
- {
- List<int> result = new List<int>();
- Node curr = this;
- while (curr != null)
- {
- result.Add(curr.Data);
- curr = curr.Next;
- }
- return String.Join(" -> ", result) + " -> null";
- }
- }
Add Comment
Please, Sign In to add comment