publicclassCode01_ReverseList{publicstaticclassNode{publicint value;publicNode next;publicNode(int data){
value = data;}}publicstaticclassDoubleNode{publicint value;publicDoubleNode last;publicDoubleNode next;publicDoubleNode(int data){
value = data;}}/**
* 单链表反转
*/publicstaticNodereverseLinkedList(Node head){Node pre =null;Node next =null;while(head !=null){
next = head.next;
head.next = pre;
pre = head;
head = next;}return pre;}/**
* 双向链表反转
*/publicstaticDoubleNodereverseDoubleList(DoubleNode head){DoubleNode pre =null, next =null;while(head !=null){
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;}return pre;}// for testpublicstaticNodegenerateRandomLinkedList(int len,int value){int size =(int)(Math.random()* len)+1;
size--;Node head =newNode((int)(Math.random()* value)+1);Node pre = head;while(size !=0){Node cur =newNode((int)(Math.random()* value)+1);
pre.next = cur;
pre = cur;
size--;}return head;}// for testpublicstaticList<Integer>getLinkedListOriginOrder(Node head){List<Integer> list =newArrayList<>();while(head !=null){
list.add(head.value);
head = head.next;}return list;}// 单链表对数器publicstaticbooleancheckLinkedListReversed(List<Integer> list,Node head){for(int i = list.size()-1; i >=0; i--){if(!list.get(i).equals(head.value)){returnfalse;}
head = head.next;}returntrue;}// for testpublicstaticDoubleNodegenerateRandomDoubleList(int len,int value){int size =(int)(Math.random()* len)+1;
size--;DoubleNode head =newDoubleNode((int)(Math.random()* value)+1);DoubleNode pre = head;while(size !=0){DoubleNode cur =newDoubleNode((int)(Math.random()* value)+1);
pre.next = cur;
cur.last = pre;
pre = cur;
size--;}return head;}// for testpublicstaticList<Integer>getDoubleListOriginOrder(DoubleNode head){List<Integer> list =newArrayList<>();while(head !=null){
list.add(head.value);
head = head.next;}return list;}// 双端链表对数器publicstaticbooleancheckDoubleListReversed(List<Integer> list,DoubleNode head){DoubleNode end =null;for(int i = list.size()-1; i >=0; i--){if(!list.get(i).equals(head.value)){returnfalse;}
end = head;
head = head.next;}// 尾部for(int i =0; i < list.size(); i++){if(!list.get(i).equals(end.value)){returnfalse;}
end = end.last;}returntrue;}publicstaticvoidmain(String[] args){int len =50;int value =100;int testTime =100000;for(int i =0; i < testTime; i++){Node node =generateRandomLinkedList(len, value);List<Integer> list1 =getLinkedListOriginOrder(node);
node =reverseLinkedList(node);if(!checkLinkedListReversed(list1, node)){System.out.println("Oops1");}DoubleNode doubleNode =generateRandomDoubleList(len, value);List<Integer> list2 =getDoubleListOriginOrder(doubleNode);
doubleNode =reverseDoubleList(doubleNode);if(!checkDoubleListReversed(list2, doubleNode)){System.out.println("Oops2");}}}}
2 链表实现队列和栈
publicclassCode02_LinkedListToQueueAndStack{publicstaticclassNode<V>{publicV value;publicNode<V> next;publicNode(V v){
value = v;
next =null;}}publicstaticclassMyQueue<V>{Node<V> head;Node<V> tail;int size;publicintsize(){return size;}publicbooleanisEmpty(){return size ==0;}publicvoidoffer(V value){Node<V> cur =newNode<>(value);if(tail ==null){
head = cur;
tail = cur;}else{
tail.next = cur;
tail = cur;}
size++;}publicVpoll(){if(head ==null){returnnull;}
size--;V value = head.value;
head = head.next;// 最后一个节点if(head ==null){
tail =null;}return value;}publicVpeek(){return head ==null?null: head.value;}}publicstaticclassMyStack<V>{Node<V> head;int size;publicintsize(){return size;}publicbooleanisEmpty(){return size ==0;}publicvoidpush(V value){Node<V> cur =newNode<>(value);if(head ==null){
head = cur;}else{
cur.next = head;
head = cur;}
size++;}publicVpop(){if(head ==null){returnnull;}
size--;V value = head.value;
head = head.next;return value;}publicVpeek(){return head ==null?null: head.value;}}// 对数器测试publicstaticvoidtestQueue(){MyQueue<Integer> myQueue =newMyQueue<>();Queue<Integer> testQueue =newLinkedList<>();int testTime =5000000;int maxValue =200000000;System.out.println("队列测试开始!!");for(int i =0; i < testTime; i++){if(myQueue.size != testQueue.size()){System.out.println("Oops");break;}double random =Math.random();if(random <0.33){int value =(int)(Math.random()* maxValue);
myQueue.offer(value);
testQueue.offer(value);}elseif(random <0.66){if(!myQueue.isEmpty()){if(!myQueue.poll().equals(testQueue.poll())){System.out.println("Oops");break;}}}else{if(!myQueue.isEmpty()){if(!myQueue.peek().equals(testQueue.peek())){System.out.println("Oops");break;}}}}if(myQueue.size != testQueue.size()){System.out.println("Oops");return;}while(!myQueue.isEmpty()){if(!myQueue.poll().equals(testQueue.poll())){System.out.println("Oops");break;}}System.out.println("队列测试结束!!");}// 对数器测试publicstaticvoidtestStack(){MyStack<Integer> myStack =newMyStack<>();Stack<Integer> testStack =newStack<>();int testTime =5000000;int maxValue =200000000;System.out.println("栈测试开始!!");for(int i =0; i < testTime; i++){if(myStack.size != testStack.size()){System.out.println("Oops");break;}double random =Math.random();if(random <0.33){int value =(int)(Math.random()* maxValue);
myStack.push(value);
testStack.push(value);}elseif(random <0.66){if(!myStack.isEmpty()){if(!myStack.pop().equals(testStack.pop())){System.out.println("Oops");break;}}}else{if(!myStack.isEmpty()){if(!myStack.peek().equals(testStack.peek())){System.out.println("Oops");break;}}}}if(myStack.size != testStack.size()){System.out.println("Oops");return;}while(!myStack.isEmpty()){if(!myStack.pop().equals(testStack.pop())){System.out.println("Oops");break;}}System.out.println("栈测试结束!!");}publicstaticvoidmain(String[] args){testQueue();System.out.println("-----------------------------");testStack();}}
3 双端队列
publicclassCode03_DoubleLinkedListToDeque{publicstaticclassNode<V>{publicV value;publicNode<V> last;publicNode<V> next;publicNode(V v){
value = v;
last =null;
next =null;}}publicstaticclassMyDeque<V>{Node<V> head;Node<V> tail;int size;publicintsize(){return size;}publicbooleanisEmpty(){return size ==0;}publicvoidofferTail(V value){Node<V> cur =newNode<>(value);if(tail ==null){
head = cur;
tail = cur;}else{
tail.next = cur;
cur.last = tail;
tail = cur;}
size++;}publicVpollHead(){if(head ==null){returnnull;}
size--;V value = head.value;if(head == tail){
head =null;
tail =null;}else{
head = head.next;
head.last =null;}return value;}publicVpeekHead(){return head ==null?null: head.value;}publicvoidofferHead(V value){Node<V> cur =newNode<>(value);if(head ==null){
head = cur;
tail = cur;}else{
cur.next = head;
head.last = cur;
head = cur;}
size++;}publicVpollTail(){if(tail ==null){returnnull;}
size--;V value = tail.value;if(head == tail){
head =null;
tail =null;}else{
tail = tail.last;
tail.next =null;}return value;}publicVpeekTail(){return tail ==null?null: tail.value;}}publicstaticvoidtestMyDeque(){MyDeque<Integer> myDeque =newMyDeque<>();Deque<Integer> testDeque =newLinkedList<>();int testTime =5000000;int maxValue =200000000;System.out.println("双端测试开始!");for(int i =0; i < testTime; i++){if(myDeque.size != testDeque.size()){System.out.println("Oops");break;}double random =Math.random();if(random <0.33){int value =(int)(Math.random()* maxValue);double rate =Math.random();if(rate <0.5){
myDeque.offerHead(value);
testDeque.offerFirst(value);}else{
myDeque.offerTail(value);
testDeque.offerLast(value);}}elseif(random <0.66){if(!myDeque.isEmpty()){double rate =Math.random();if(rate <0.5){if(!myDeque.pollHead().equals(testDeque.pollFirst())){System.out.println("Oops");break;}}else{if(!myDeque.pollTail().equals(testDeque.pollLast())){System.out.println("Oops");break;}}}}else{if(!myDeque.isEmpty()){double rate =Math.random();if(rate <0.5){if(!myDeque.peekHead().equals(testDeque.peekFirst())){System.out.println("Oops");break;}}else{if(!myDeque.peekTail().equals(testDeque.peekLast())){System.out.println("Oops");break;}}}}}if(myDeque.size != testDeque.size()){System.out.println("Oops");return;}while(!myDeque.isEmpty()){if(!myDeque.pollHead().equals(testDeque.pollFirst())){System.out.println("Oops");break;}}System.out.println("双端测试结束!");}publicstaticvoidmain(String[] args){testMyDeque();}}
4 K 个节点组内逆序调整
publicclassCode04_ReverseNodesInKGroup{publicstaticclassListNode{publicint val;publicListNode next;}publicListNodereverseKGroup(ListNode head,int k){ListNode start = head;ListNode end =getKGroupEnd(head, k);if(end ==null){return head;}// 第一组
head = end;reverse(start, end);// 上一组结尾ListNode lastEnd = start;while(lastEnd.next !=null){// 第二组开始节点
start = lastEnd.next;
end =getKGroupEnd(start, k);// 不足 k 个直接返回if(end ==null){return head;}// 第n-1组reverse(start, end);// 上一组反转的节点指向当前组反转的起始节点
lastEnd.next = end;// 更新最新的结束节点
lastEnd = start;}return head;}/**
* 获取 node 开始的第 k 的节点
*/publicListNodegetKGroupEnd(ListNode start,int k){while(k-->0&& start !=null){
start = start.next;}return start;}/**
* 翻转 start - end 的顺序
*/publicvoidreverse(ListNode start,ListNode end){
end = end.next;ListNode pre =null;ListNode cur = start;ListNode next =null;while(cur != end){
next = cur.next;// 反转
cur.next = pre;
pre = cur;
cur = next;}// 指向 end 的后一个节点
start.next = end;}}
5 两个链表相加
publicclassCode05_AddTwoNumbers{publicstaticclassListNode{publicint val;publicListNode next;publicListNode(int val){this.val = val;}publicListNode(int val,ListNode next){this.val = val;this.next = next;}}publicListNodeaddTwoNumbers(ListNode l1,ListNode l2){// 分开链表和短链表int len1 =getLength(l1);int len2 =getLength(l2);// l 长链表 s 短链表ListNode l, s;if(len1 >= len2){
l = l1;
s = l2;}else{
l = l2;
s = l1;}ListNode curL = l,
curS = s,
lastL =null;// 进位int carry =0;while(curS !=null){int sum = curL.val + curS.val + carry;
curL.val = sum %10;// 更新进位
carry = sum /10;
lastL = curL;
curL = curL.next;
curS = curS.next;}while(curL !=null){int sum = curL.val + carry;
curL.val = sum %10;// 更新进位
carry = sum /10;
lastL = curL;
curL = curL.next;}if(carry !=0){
lastL.next =newListNode(carry);}return l;}publicintgetLength(ListNode node){int len =0;while(node !=null){
len++;
node = node.next;}return len;}}