1, how to find a single list in the penultimate K elements
Two pointers, a pointer to pointer movement than the other elements k-1, and then resumes network while moving below the first next == null moves, when the second pointer points at this time is the reciprocal of the k-th element
2, how to delete duplicate data from the list
private static List<Integer> getRemoveRepeat(List<Integer> ids) { Map<Integer, Object> map = new HashMap<>(); Iterator iterator = ids.iterator(); while (iterator.hasNext()) { Integer key = Integer.valueOf(iterator.next().toString()); if (map.get(key) != null) { iterator.remove(); } else { map.put(key, new Object()); } } return ids; }
Using the map data storage, the map is a get operation o (1) operation, so quickly, but the spatial complexity will increase
3, how to find the reciprocal of a single linked list of k elements (two pointers Method)
public class LastKNodeFindTest { public static void main(String[] args) { Node last = new Node(1, null); Node last_1 = new Node(2, last); Node last_2 = new Node(3, last_1); Node last_3 = new Node(4, last_2); Node last_4 = new Node(5, last_3); Node last_5 = new Node(6, last_4); Node last_6 = new Node(7, last_5); Node findNode = findKNode(last_6, 6); System.out.println(findNode); } private static Node findKNode(Node first, int k) { int begin = 0; Node knode = first; Node knode1 = first; while (knode.next != null) { if (begin >= k - 1) { knode1 = knode1.next; } knode = knode.next; begin++; } return knode1; } static class Node { private int i; private Node next; public Node(int i, Node next) { this.i = i; this.next = next; } public int getI() { return i; } public void setI(int i) { this.i = i; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } }
How to reverse a linked list
Reverse output list
private static void revise(Node first){
if(first != null){
revise(first.next);
System.out.println(first.i);
}
}
How to find the maximum and minimum array
Dual element method is better
public class MaxMinTest { static int max = 0; static int min = 10; public static void main(String[] args) { int[] arr = {1, 23, 4, 88, 21, 53, 77, 9, 12, 1313, 77, 31}; maxmin(arr); System.out.println(max + min); } private static void maxmin(int[] arr) { for (int i = 0; i < arr.length; i = i + 2) { int tempMax = 0; int tempMin = 0; if (i + 1 > arr.length) { max = arr[i] > max ? arr[i] : max; min = arr[i] > min ? min : arr[i]; } else if (arr[i] > arr[i + 1]) { tempMax = arr[i]; tempMin = arr[i + 1]; } else { tempMax = arr[i + 1]; tempMin = arr[i]; } max = tempMax > max ? tempMax : max; min = tempMin > min ? min : tempMin; } } }