LinkedListStuff.java 664 Bytes

public class LinkedListStuff {
	private static class ListNode {
		int data;
		ListNode next;

		public ListNode(int data) {
			this(data, null);
		}

		public ListNode(int data, ListNode next) {
			this.data = data;
			this.next = next;
		}
	}

	public static boolean contains(ListNode head, int needle) {
		if (head == null) {
			return false;
		}
		else {
			int data = head.data;
			if (data == needle) {
				return true;
			}
			ListNode rest = head.next;
			boolean result = contains(rest, needle);
			return result;
		}
	}

	public static void main(String[] args) {
		System.out.println(contains(new ListNode(1, new ListNode(2, new ListNode(3))), 4));
	}
}