/**
* Created with IntelliJ IDEA.
* Description:
* User: zhuzhuzhuchao
* Date: 2022-01-04
* Time: 16:46
*/
public class TestDemo {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
MyLinkedList myLinkedList1 = new MyLinkedList();
myLinkedList.addLast(12);
myLinkedList.addLast(23);
myLinkedList.addLast(34);
myLinkedList.addLast(45);
myLinkedList.addLast(56);
myLinkedList.display();
// // 反向
// ListNode ret = myLinkedList.reverseList();
// myLinkedList.display2(ret);
// 求链表中间节点
System.out.println(myLinkedList.middleNode().val);
// 找倒数第k个节点
System.out.println(myLinkedList.FindKthToTail(2).val);
System.out.println(myLinkedList.FindKthToTail(5).val);
myLinkedList1.addLast(13);
myLinkedList1.addLast(24);
myLinkedList1.addLast(30);
ListNode ret = mergeTwoLists(myLinkedList.head,myLinkedList1.head);
myLinkedList.display2(ret);
}
// 合并两个有序链表
public static ListNode mergeTwoLists(ListNode headA,ListNode headB) {
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while (headA != null && headB != null) {
if (headA.val < headB.val) {
tmp.next = headA;
headA = headA.next;
tmp = tmp.next;
} else {
tmp.next = headB;
headB = headB.next;
tmp = tmp.next;
}
}
if (headA != null) {
tmp.next = headA;
}
if (headB != null) {
tmp.next = headB;
}
return newHead.next;
}
}
/**
* Created with IntelliJ IDEA.
* Description:
* User: zhuzhuzhuchao
* Date: 2022-01-04
* Time: 17:05
*/
public class MyLinkedList {
public ListNode head;//链表的头引用
public void createList() {
ListNode listNode1 = new ListNode(12);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(34);
ListNode listNode4 = new ListNode(45);
ListNode listNode5 = new ListNode(56);
this.head = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
}
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public void display2(ListNode newHead) {
ListNode cur = newHead;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
ListNode listNode = new ListNode(1);
}
//头插法
public void addFirst(int data) {
ListNode listNode = new ListNode(data);
listNode.next = this.head;
this.head = listNode;
}
//尾插法
// 第一次插入必须判断是不是空
// public void addLast(int data){
// ListNode cur = this.head;
// ListNode listNode = new ListNode(data);
// if (cur == null) {
// cur.val = data;
// } else {
// while (cur.next != null) {
// cur = cur.next;
// }
// cur.next = listNode;
// }
// }
// 博哥写
public void addLast(int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head;
if (this.head == null) {
this.head = node;
} else {
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
if (index < 0 || index > size()) {
System.out.println("位置不合法");
return;
}
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
ListNode cur = findIndex(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
public ListNode findIndex(int index) {
ListNode cur = this.head;
while (index - 1 != 0) {
cur = cur.next;
index--;
}
return cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (this.head == null) {
System.out.println("空");
return;
}
if (this.head.val == key) {
this.head = this.head.next;
return;
}
ListNode prev = searchPerv(key);
if (prev == null) {
System.out.println("没有");
return;
}
ListNode del = prev.next;
prev.next = del.next;
}
public ListNode searchPerv(int key) {
ListNode cur = this.head;
while (cur.next != null) {
if (cur.next.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
//删除所有值为key的节点
public ListNode removeAllKey(int key) {
if (this.head == null) {
return null;
}
ListNode prev = this.head;
ListNode cur = this.head.next;
while (cur != null) {
if (cur.val == key) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
// 最后处理头
if (this.head.val == key) {
this.head = this.head.next;
}
return this.head;
}
//得到单链表的长度
public int size() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
public void clear() {
// 粗暴
// this.head = null;
while (this.head != null) {
ListNode curNext = this.head.next;
this.head.next = null;
this.head = curNext;
}
this.head = null;
}
// 换方向
public ListNode reverseList() {S
if (this.head == null) {
return null;
}
ListNode cur = this.head;
ListNode prev = null;
while (cur != null) {
ListNode curNext = cur.next;
cur.next = prev;
prev = cur;
cur = curNext;
}
return prev;
}
// 给定一个带有头结点 head 的非空单链表,
// 返回链表的中间结点。
// 如果有两个中间结点,则返回第二个中间结点。
public ListNode middleNode() {
if (head == null) {
return null;
}
ListNode fast = this.head;
ListNode slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
// 找到倒数第k个节点
// size - k步
// 能不能遍历链表一遍
// fast先走k-1步
public ListNode FindKthToTail(int k) {
ListNode fast = this.head;
ListNode slow = this.head;
if (k <= 0 || this.head == null) {
return null;
}
// for (int i = 0; i < k-1; i++) {
// if (fast.next == null) {
// return null;
// }
// fast = fast.next;
// }
// 博哥
while (k - 1 != 0) {
fast = fast.next;
if (fast == null) {
return null;
}
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
// 大于X放前面
public ListNode partition(int x) {
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
ListNode cur = this.head;
while (cur != null) {
if (cur.val < x) {
// 第一次和不是第一次
if (bs == null) {
bs = cur;
be = cur;
} else {
be.next = cur;
be = be.next;
}
} else {
if (as == null) {
as = cur;
ae = cur;
} else {
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next;
}
if (bs == null) {
return as;
}
be.next = as;
if (as != null) {
ae.next = null;
}
return bs;
}
// 删除链表中重复的节点(有序)
public ListNode deleteDuplication() {
ListNode cur = head;
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while (cur != null) {
if (cur.next != null && cur.val == cur.next.val) {
while (cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
cur = cur.next;
} else {
tmp.next = cur;
tmp = tmp.next;
cur = cur.next;
}
}
tmp.next = null;
return newHead.next;
}
}
// ListNode代表一个节点
// 单向不带头非循环
// 不带头:头一直在变
// 循环:最后一个的next是第一个的地址
class ListNode {
public int val;
public ListNode next;//存节点地址
public ListNode(int val) {
this.val = val;
}
}