Created
October 11, 2017 04:38
-
-
Save khayyamsaleem/7b1f76a5a7df44302fd3a05f4eed64c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LL<E>{ | |
private static class Node<E>{ | |
E data; | |
Node<E> next; | |
public Node(E data, Node<E> next){ | |
this.data = data; | |
this.next = next; | |
} | |
public Node(E data){ | |
this.data = data; | |
this.next = null; | |
} | |
public void append(E elem){ | |
Node<E> t = this; | |
while(t.next != null) | |
t = t.next; | |
t.next = new Node<>(elem); | |
} | |
public Node<E> append(Node<E> n){ | |
Node<E> t = this; | |
while(t.next != null) | |
t = t.next; | |
t.next = n; | |
return this; | |
} | |
public String toString(){ | |
Node<E> t = this; | |
String out = "[ "; | |
while(t!= null){ | |
out += t.data.toString() + " "; | |
t=t.next; | |
} | |
return out + "]"; | |
} | |
} | |
public static <E> Node<E> iter_repeatLN(Node<E> node, int n){ | |
Node<E> out = new Node<>(node.data); | |
for(int i = 0; i < n; i++){ | |
Node<E> cur = node; | |
while(cur != null){ | |
out.append(cur.data); | |
cur = cur.next; | |
} | |
} | |
return out.next; | |
} | |
public static <E> Node<E> rec_repeatLN(Node<E> node, int n){ | |
Node<E> out = new Node<>(node.data); | |
Node<E> t = node; | |
while(t.next != null){ | |
out.append(t.next.data); | |
t = t.next; | |
} | |
if(n == 0) | |
return null; | |
return out.append(rec_repeatLN(node, n-1)); | |
} | |
public static void main(String[] args){ | |
Node<Integer> n = new Node<>(1); | |
n.append(2); | |
n.append(3); | |
System.out.println(n); | |
System.out.println(rec_repeatLN(n, 3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment