*/
static void delete(int[] a, int n, int x) { // preconditions: [0] <= ... <= a[n-1], and n <= a.length; // postconditions: a[0] <= ... <= a[n-2], and x is deleted; int i = 0; // find the first index i for which a[i] > x: while (i < n && a[i] <= x) { ++i; } // shift {a[i],...,a[n-1]} into {a[i-1],...,a[n-2]}: if (i < n - 1) { System.arraycopy(a, i, a, i - 1, n - i); } a[n - 1] = 0; }/**
static int size(Node list) { int size = 0; while (list != null) { ++ size; list = list.next; } return size; }/**
static int sum(Node list){ int sum = 0; while(list != null){ sum += list.data; list = list.next; } return sum; }/**
void removeLast(Node list){ if(list == null || list.next == null) { //throw new IllegalStatException(); } //{33,55,77,99} while(list.next.next != null) { list = list.next; } list.next = null; }/**
static Node copy(Node list) { if(list == null) { return null; } Node clone = new Node(list.data); for (Node p = list, q = clone; p != null; p = p.next, q = q.next) { q.next = p.next; } return clone; }/**
Node sublist(Node list, int m, int n) { if (m < 0 || n < m) { throw new IllegalArgumentException(); } else if (n == m) { return null; } //55,22,11,33 for (int i = 0; i < m; i++) { list = list.next; } Node clone = new Node(list.data); Node p = list, q = clone; for (int i = m + 1; i < n; i++) { if (p.next == null) { throw new IllegalArgumentException(); } q.next = new Node(p.next.data); p = p.next; q = q.next; } return clone; }
Some useful methods about linkedList.,布布扣,bubuko.com
Some useful methods about linkedList.
原文:http://blog.csdn.net/sxb0841901116/article/details/34219039