/**
* Created with Intellij IDEA
* ClassName:MyLinkedList
* User:MaLe
* Description:单链表
* @Date:2021/10/23
* @Time:9:40
* @author:395645313@qq.com
*/
class Node {
public int val;//0
public Node next;//null
public Node(int val) {
this.val = val;
}
}
public class MyLinkedList {
public Node head;//单链表的头节点的引用
public void createLinkedList() {
Node node1 = new Node(12);
Node node2 = new Node(23);
Node node3 = new Node(34);
Node node4 = new Node(45);
node1.next = node2;
node2.next = node3;
node3.next = node4;
this.head = node1;
}
public void show() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
public void show2(Node newHead) {
Node cur = newHead;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
public int getSize() {
int count = 0;
Node cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
Node cur=this.head;
while (cur!=null){
if (cur.val==key) {
return true;
}
cur = cur.next;
}
return false;
}
//头插法
public void addFirst(int data) {
Node cur = new Node(data);
cur.next=this.head;
this.head=cur;
}
//尾插法
public void addLast(int data) {
Node node = new Node(data);
Node cur = this.head;
if (this.head==null){
this.head=node;
return;
}
while (cur.next!=null){
cur=cur.next;
}
cur.next=node;
}
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data) {
Node node = new Node(data);
Node cur=this.head;
if (index<0 || index>getSize()){
System.out.println("index输入不合法");
return false;
}
if (index==0){
addFirst(data);
return true;
}
if (index==getSize()){
addLast(data);
return true;
}
for (int i = 0; i <index-1; i++) {
cur=cur.next;
}
node.next=cur.next;
cur.next=node;
return true;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (head.val==key){
this.head=this.head.next;
return;
}
Node prev=searchPrev(key);
if (prev==null){
System.out.println("没有你要删除的结点");
return;
}else{
prev.next=prev.next.next;
}
}
//寻找要删除的元素key
private Node searchPrev(int key){
Node cur=this.head;
while (cur.next!=null){
if (cur.next.val==key) {
return cur;
}
cur=cur.next;
}
return null;
}
//删除所有值为key的节点
public Node removeAllKey(int key) {
judge();
Node cur=this.head.next;
Node prev=this.head;
while (cur!=null){
if (cur.val==key){
prev.next=cur.next;
}else {
prev=cur;
}
cur=cur.next;
}
if (this.head.val==key){
this.head=this.head.next;
}
return head;
}
public void clear() {
this.head=null;
}
//反转单链表
public Node reverseList(){
judge();
Node prev=null;
Node curNext=null;
Node cur=this.head;
while (cur!=null){
curNext=cur.next;
cur.next=prev;
prev=cur;
cur=curNext;
}
return prev;
}
//判断单链表是否为空
private Node judge() {
if (this.head == null) {
return null;
}
return head;
}
//寻找该单链表中间vel值(有两个取第二个)
public Node middle () {
judge();
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
//寻找该单链表的倒数第N个元素
public Node rec (int n) {
judge();
Node fast = this.head;
Node slow = this.head;
if (n<=0 ){
System.out.println("没有您要找的元素!!");
return null;
}
while ( n-1!=0){
if (fast==null){
System.out.println("没有您要找的元素!!");
return null;
}
fast=fast.next;
n--;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
//将两个(有序)单链表,和并成一个有序单链表
public Node merge (Node headA,Node headB) {
Node newHead=new Node(-1);
Node tmp=newHead;
while (headA!=null && headB!=null){
if (headA.val<headB.val){
tmp.next=headA;
headA=headA.next;
}else{
tmp.next=headB;
headB=headB.next;
}
tmp=tmp.next;
}
if (headA==null){
tmp.next=headB;
}else {
tmp.next=headA;
}
return newHead.next;
}
//输入一个n,将链表中val值 小于n的放在前边,大于的放在后边。(并且不打乱原来顺序)
public Node partition(int n){
Node cur=this.head;
Node as=null;
Node ae=null;
Node bs=null;
Node be=null;
while (cur!=null){
if (cur.val<n){
if (as==null){
as=cur;
ae=cur;
}else {
as.next=cur;
ae=ae.next;
}
}else {
if (cur.val<n){
bs=cur;
be=cur;
}else {
bs.next=cur;
be=be.next;
}
}
cur=cur.next;
}
if (as==null){
return bs;
}
ae.next=bs;
if (bs!=null){
be.next=null;
}
return as;
}
public static void main (String[]args){
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(5);
myLinkedList.addLast(8);
myLinkedList.addLast(9);
myLinkedList.addLast(10);
myLinkedList.show();
MyLinkedList myLinkedList1 = new MyLinkedList();
myLinkedList1.addLast(2);
myLinkedList1.addLast(3);
myLinkedList1.addLast(11);
myLinkedList1.addLast(12);
myLinkedList1.show();
Node ret = myLinkedList.partition(5);
myLinkedList1.show2(ret);
}
}
单链表代码
最新推荐文章于 2024-09-29 18:52:49 发布