public class StringNode { private char ch; private StringNode next; public StringNode(char c, StringNode n) { ch = c; next = n; } private static StringNode getNode(StringNode str, int i) { if (i < 0 || str == null) { return null; } else if (i == 0) { return str; } else { return getNode(str.next, i - 1); } } public static StringNode convert(String s) { // code to check for empty string goes here StringNode firstNode = new StringNode(s.charAt(0), null); StringNode prevNode = firstNode; StringNode nextNode; for (int i = 1; i < s.length(); i++) { nextNode = new StringNode(s.charAt(i), null); prevNode.next = nextNode; prevNode = nextNode; } return firstNode; } public static StringNode copy(StringNode str) { if (str == null) { return null; } StringNode copyRest = copy(str.next); return new StringNode(str.ch, copyRest); } // for simplicity, this version assumes the inputs are valid public static StringNode deleteChar(StringNode str, int i) { if (i == 0) { str = str.next; } else { StringNode prevNode = getNode(str, i-1); prevNode.next = prevNode.next.next; } return str; } public static void main(String[] args) { StringNode s1 = StringNode.convert("cat"); StringNode s2 = s1; StringNode s3 = StringNode.copy(s1); s1 = StringNode.deleteChar(s1, 1); } }