public static void main(String[] args) { ListNode a = new ListNode('1'); ListNode b = new ListNode('2'); ListNode c = new ListNode('3'); ListNode d = new ListNode('1'); ListNode e = new ListNode(' '); ListNode f = new ListNode('3'); a.next = b; b.next = c; c.next = d; d.next = e; e.next = f; System.out.println(a); removeDupes(a); System.out.println("After removing duplicate ListNodes: "); System.out.println(a); }
public ListNode partition(ListNode head, int x) { ListNode headOfHead = new ListNode(-1); headOfHead.next = head; ListNode p = headOfHead; while (p.next != null) { p = p.next; // CUT begin debug(p.val); // CUT end } ListNode tail = new ListNode(-1); p.next = tail; p = headOfHead; ListNode q = tail; while (p.next != tail) { if (p.next.val < x) { p = p.next; } else { q.next = p.next; q = q.next; p.next = p.next.next; q.next = null; } } p.next = tail.next; return headOfHead.next; }
public ListNode deleteDuplicates(ListNode a) { ListNode temp = a; LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>(); int i = 0; while (temp != null) { if (map.get(temp.val) == null) { map.put(temp.val, 1); } else { map.put(temp.val, map.get(temp.val) + 1); } temp = temp.next; } System.out.println(map); Iterator<Integer> it = map.keySet().iterator(); ListNode root = null; ListNode root1 = null; while (it.hasNext()) { int x = it.next(); if (map.get(x) == 1) { if (root == null) { root = new ListNode(x); root1 = root; } else { ListNode node = new ListNode(x); root1.next = node; root1 = node; } } } return root; }
/* Solution 2 Did not use additional space, each time encounter the first unique value node, traversal the rest of the list to delete the same value's nodes. Assumption: None. Time complexity: O(n^2) Space Complexity: O(1) */ private static ListNode RemoveDups2(ListNode head) { if (head == null) return head; ListNode pre = head; while (pre != null) { ListNode pre2 = pre; ListNode cur = pre.next; int target = pre.val; while (cur != null) { // delete the dups in the rest of the list if (cur.val == target) { pre2.next = cur.next; cur = cur.next; } else { pre2 = cur; cur = cur.next; } } // move to next different value's node pre = pre.next; } return head; }
/* Helpers */ private static InputArgs input() throws IOException { System.out.println("2.2 Return Kth to Last"); // read K System.out.print("Please input K: "); Scanner sc = new Scanner(System.in); int k = sc.nextInt(); // read number of nodes System.out.println(); System.out.print("Please input the number of nodes in the linked list: "); int n = sc.nextInt(); // read linked list System.out.println(); System.out.print("Please input value of nodes (seperate by space, e.g., 1 2 3): "); ListNode dummy = new ListNode(0); ListNode pre = dummy; for (int i = 0; i < n; i++) { ListNode cur = new ListNode(sc.nextInt()); pre.next = cur; pre = cur; } return new InputArgs(k, dummy.next); }
public int search(Object p) { int index = 0; boolean flag = false; temp = back; ListNode temp2 = new ListNode(p, null); if (back == null) { return -1; } // else if case else { while (temp.getNext() != null && flag == false) { if (temp.getValue() == temp2.getValue()) { flag = true; } else { temp = temp.getNext(); index++; } } if (flag) { return index; } else { return -1; } } }
public ListNode removeLast() { if (front == null) { return null; } else { ListNode temp2 = back; while (temp2.getNext() != front && temp2.getNext() != null) // second condition test { temp2 = temp2.getNext(); } temp = front; front = temp2; size--; return temp; } }
public void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int x = in.nextInt(); ListNode head = new ListNode(-1); ListNode p = head; for (int i = 0; i < n; i++) { p.next = new ListNode(in.nextInt()); p = p.next; // out.println(p.val); } ListNode res = new A1().partition(head.next, x); p = res; while (p != null) { out.println(p.val); p = p.next; } }
public static void removeDupes(ListNode ListNode) { Set<Integer> set = new HashSet<Integer>(); // add the first ListNode set.add(ListNode.val); ListNode prev = ListNode; ListNode curr = ListNode.next; while (curr != null) { // already exists, delete this duplicate one if (set.contains(curr.val)) { prev.next = curr.next; } else { set.add(curr.val); prev = curr; } curr = curr.next; } }
public void insertLast(ListNode x) { if (front == null) { front = back = x; size++; } else { front.setNext(x); front = x; size++; } }
public void insertFirst(ListNode x) { if (back == null) { front = back = temp = x; size++; } else { x.setNext(back); back = x; size++; } }
/* Solution 1 Using hashtable to record nodes, delete the node if the same value already exists. Assumption: None. Time complexity: O(n) Space Complexity: O(n) */ private static ListNode RemoveDups1(ListNode head) { if (head == null) return head; Set<Integer> set = new HashSet<Integer>(); ListNode pre = head; ListNode cur = head.next; set.add(head.val); while (cur != null) { if (set.contains(cur.val)) { // delete dup pre.next = cur.next; cur = cur.next; } else { set.add(cur.val); pre = cur; cur = cur.next; } } return head; }
public ListNode removeFirst() { if (back == null) { return null; } else { temp = back; back = back.getNext(); size--; return temp; } }
// input private static ListNode input() { ListNode dummyhead = new ListNode(0); ListNode pre = dummyhead; System.out.println("/**** 2.1 Remove Dups ****/"); System.out.println("Please input a linked list (using space to seperate nodes): "); try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); String[] nodes = s.split(" "); for (int i = 0; i < nodes.length; i++) { ListNode cur = new ListNode(Integer.parseInt(nodes[i])); pre.next = cur; pre = cur; } } catch (IOException e) { System.out.println("input error, " + e.getMessage()); } return dummyhead.next; }