一 单链表的反转
public static class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
public static Node reverseLinkedList(Node head) {
Node pre = null;
Node next = null;
while (head != null){
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
二 双链表的反转
public static class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int value) {
this.value = value;
}
}
public static DoubleNode reverseDoubleList(DoubleNode head){
DoubleNode pre = null;
DoubleNode next = null;
while (head != null){
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;
}
return pre;
}
三 用单链表实现队列和栈
实现队列
public static class Node<V> {
public V value;
public Node<V> next;
public Node(V v) {
value = v;
next = null;
}
}
public static class MyQueue<V> {
public Node<V> head;
public Node<V> tail;
private int size;
public MyQueue() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void offer(V value) {
Node<V> cur = new Node<>(value);
if (tail == null) {
head = cur;
tail = cur;
} else {
tail.next = cur;
tail = cur;
}
size++;
}
public V poll() {
V ans = null;
if (head != null) {
ans = head.value;
head = head.next;
size--;
}
if (head == null) {
tail = null;
}
return ans;
}
public V peek() {
V ans = null;
if (head != null) {
ans = head.value;
}
return ans;
}
}
实现队列
public static class Node<V> {
public V value;
public Node<V> next;
public Node(V v) {
value = v;
next = null;
}
}
public static class MyStack<V> {
private Node<V> head;
private int size;
public MyStack() {
head = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void push(V value) {
Node<V> cur = new Node<>(value);
if (head == null) {
head = cur;
} else {
cur.next = head;
head = cur;
}
size++;
}
public V pop() {
V ans = null;
if (head != null) {
ans = head.value;
head = head.next;
size--;
}
return ans;
}
public V peek() {
return head != null ? head.value : null;
}
}
四 用双链表实现
实现双端队列
package com.liu.class04;
public class Code03_DoubleLinkedListToDeque {
public static class Node<V> {
public V value;
public Node<V> last;
public Node<V> next;
public Node(V v) {
value = v;
last = null;
next = null;
}
}
public static class MyDeque<V> {
private Node<V> head;
private Node<V> tail;
private int size;
public MyDeque() {
head = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void pushHead(V value) {
Node<V> cur = new Node<>(value);
if (head == null) {
head = cur;
tail = cur;
} else {
cur.next = head;
head.last = cur;
head = cur;
}
size++;
}
public void pushTail(V value) {
Node<V> cur = new Node<>(value);
if (head == null) {
head = cur;
tail = cur;
} else {
tail.next = cur;
cur.last = tail;
tail = cur;
}
size++;
}
public V popHead() {
V ans = null;
if(head==null){
return ans;
}
size--;
ans = head.value;
if(head==tail){
head = null;
tail = null;
}else {
head = head.next;
head.last = null;
}
return ans;
}
public V popTail() {
V ans = null;
if(head==null){
return ans;
}
size--;
ans = tail.value;
if(head == tail){
head = null;
tail = null;
}else {
tail.last.next = null;
}
return ans;
}
}
}
五 K个节点的组内逆序调整
public class Code04_ReverseNodesInKGroup {
public static class ListNode{
public int val;
public ListNode next;
}
public static ListNode reverseKGroup(ListNode head, int k) {
ListNode start = head;
ListNode end = getGroupEnd(start, k);
if(end == null){
return head;
}
//第一组凑齐了
head = end;
reverse(start,end);
//上一组的结尾节点
ListNode lastEnd = start;
while (lastEnd.next!=null){
start = lastEnd.next;
end = getGroupEnd(start, k);
if (end == null){
return head;
}
reverse(start,end);
lastEnd.next = end;
lastEnd = start;
}
return head;
}
public static ListNode getGroupEnd(ListNode start, int k) {
while (--k!=0 && start !=null){
start =start.next;
}
return start;
}
public static void reverse(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;
}
start.next = end;
}
}
六 两个链表相加
这个分三种情况讨论
第一种情况
考虑进位
七 两个有序链表的合并
public class Code06_MergeTwoSortedLinkedList {
public static class ListNode {
public int val;
public ListNode next;
}
public static ListNode mergeTwoList(ListNode head1, ListNode head2) {
if (head1 == null || head2 == null) {
return head1 == null ? head2 : head1;
}
ListNode head = head1.val <= head2.val ? head1 : head2;
ListNode cur1 = head.next;
ListNode cur2 = head == head1 ? head2 : head1;
ListNode pre = head;
while (cur1!=null && cur2!=null){
if(cur1.val <= cur2.val){
pre.next = cur1;
cur1 = cur1.next;
}else {
pre.next = cur2;
cur2 = cur2.next;
}
pre = pre.next;
}
pre.next = cur1!=null?cur1:cur2;
return head;
}
}
八 删除链表中指定值的所有节点
public class Code07_removeValues {
public static class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
public static Node removeValues(Node head, int num) {
while (head != null) {
if (head.value != num) {
break;
}
head = head.next;
}
//head来到第一个不需要删除的地方
Node pre = head;
Node cur = head;
while (cur != null) {
if (cur.value == num) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
}