diff --git a/src/ListNode.java b/src/ListNode.java index dd6028c..a561d78 100644 --- a/src/ListNode.java +++ b/src/ListNode.java @@ -1,3 +1,13 @@ public class ListNode { + + // public static void main(String[] args){ + // ListNode myNode = new ListNode(); + // myNode.data = 'x'; + + + // System.out.println(myNode.data); + // } + public char data; + public ListNode next; } \ No newline at end of file diff --git a/src/Practice.java b/src/Practice.java index 34a2f8d..7cd8e4e 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,128 @@ 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; //held x + + ListNode extra = moreNode; + moreNode.data = 'e'; + + System.out.println(extra.data); + + // ListNode extra = moreNode; + // moreNode.data = 'e'; + + + ListNode hello = new ListNode(); + hello.data = 'x'; + extra.next = hello; + // printList(coolNode); + + //int XCount = countX(coolNode); + //System.out.println(xCount); + + ListNode newHead = removeAt(coolNode, 2); + //System.out.println + printList(newHead); + } + + // int xCount = countX(coolNode); + // System.out.println(xCount); + + + //System.out.println(myNode.next.next.data); + // System.out.println(myNode.data); + // System.out.println(anotherNode.data); + + public static void printList(ListNode head){ + //starting from head + + //My Attempt + //data.next(); ** + + //System.out.println() ** + + + //print all data in list in order + + + + //until end of list + // //print out of data at current + //move current to the next node + + + 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){ + + int count = 0; + + ListNode current = head; + while(current != null){ + if(current.data == 'x'){ + count++; + } + current = current.next; + } + return count; + } + + //count.node(x); M.A + + + + //remove the node at removeIndex, and return the head of the list + //example: + // e -> t -> k -> y + //removeIndex 2 + // e -> t -> y + 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; + + + } + + }