diff --git a/src/ListNode.java b/src/ListNode.java index dd6028c..8293484 100644 --- a/src/ListNode.java +++ b/src/ListNode.java @@ -1,3 +1,4 @@ public class ListNode { - + public char data; + public ListNode next; } \ No newline at end of file diff --git a/src/Practice.java b/src/Practice.java index 34a2f8d..aa6ef6f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,77 @@ public class Practice { public static void main(String[] args) { + ListNode myNode = new ListNode(); + myNode.data = 'x'; + ListNode anotherNode = new ListNode(); + anotherNode.data = 't'; + + myNode.next = anotherNode; + + ListNode moreNode = new ListNode(); + moreNode.data = 'r'; + + anotherNode.next = moreNode; + + ListNode coolNode = new ListNode(); + coolNode.data = 'w'; + coolNode.next = myNode; + + ListNode extra = moreNode; + moreNode.data = 'e'; + + ListNode hello = new ListNode(); + hello.data = 'x'; + extra.next = hello; + + //printList(coolNode); + //System.out.println(countX(coolNode)); + + ListNode newHead = removeAt(coolNode, 0); + printList(newHead); + + } + + public static void printList(ListNode head) { + ListNode current = head; + + while (current != null) { + System.out.println(current.data); + current = current.next; + } + } + + // Count how many nodes hold an x + public static int countX(ListNode head) { + ListNode current = head; + int xCount = 0; + + while (current != null) { + if (current.data == 'x') { + xCount++; + } + current = current.next; + } + + return xCount; + + } + +//remove the node at removeIndex, and return the head of the list. +//example: +//e -> t -> k -> y -z +//removeIndex 2 +// + public static ListNode removeAt(ListNode head, int removeIndex){ + if(removeIndex==0){ + return head.next; + } + ListNode current = head; + for(int i=0; i < removeIndex - 1 ;i++){ + current = current.next; + } + current.next = current.next.next; + + return head; } }