代码实现
package Java_1230;
class Node {
public int val;
public Node next;
public Node ( ) {
}
public Node ( int val) {
this . val = val;
}
}
public class MyLinkedList {
public Node head;
public void createLinked ( ) {
this . head = new Node ( 12 ) ;
Node node2 = new Node ( 22 ) ;
Node node3 = new Node ( 32 ) ;
Node node4 = new Node ( 42 ) ;
head. next = node2;
node2. next = node3;
node3. next = node4;
}
public void display ( ) {
Node cur = this . head;
while ( cur!= null) {
System. out. print ( cur. val+ " " ) ;
cur = cur. next;
}
System. out. println ( ) ;
}
public Node findLastNode ( ) {
Node cur = this . head;
while ( cur. next!= null) {
cur = cur. next;
}
return cur;
}
public int Size ( ) {
Node cur = this . head;
int count= 0 ;
while ( cur!= null) {
count++ ;
cur = cur. next;
}
return count;
}
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 node = new Node ( data) ;
if ( this . head== null) {
this . head = node;
} else {
node. next = this . head;
this . head = node;
}
}
public void addLast ( int data) {
Node node= new Node ( data) ;
if ( this . head== null) {
this . head= node;
} else {
findLastNode ( ) . next = node;
}
}
public void addIndex ( int index, int data) {
if ( index< 0 || index> Size ( ) ) {
System. out. println ( "index不合法" ) ;
}
if ( index== 0 ) {
addFirst ( data) ;
return ;
}
if ( index== Size ( ) - 1 ) {
addLast ( data) ;
return ;
}
Node cur= moveIndex ( index) ;
Node node = new Node ( data) ;
node. next= cur. next;
cur. next = node;
}
public Node moveIndex ( int index) {
Node cur = this . head;
int count = 0 ;
while ( count != index- 1 ) {
cur = cur. next;
count++ ;
}
return cur;
}
public Node searchPrev ( int key) {
Node cur = this . head;
while ( cur. next!= null) {
if ( cur. next. val == key) {
return cur;
}
cur = cur. next;
}
return null;
}
public void remove ( int key) {
if ( this . head== null) {
return ;
}
if ( this . head. val== key) {
this . head = this . head. next;
return ;
}
Node cur = searchPrev ( key) ;
if ( cur == null) {
System. out. println ( "删除失败,没有Key的前驱" ) ;
} else {
Node del = cur. next;
cur. next = del. next;
}
}
public void removeAllKey ( int key) {
if ( this . head== null) {
return ;
}
Node prev = this . head;
Node cur = prev. next;
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;
}
}
public void clear ( ) {
this . head = null;
}
public Node reverseList ( ) {
Node cur = this . head;
Node prev = null;
Node newHead = null;
while ( cur!= null) {
Node curNext = cur. next;
if ( curNext== null) {
newHead = cur;
}
cur. next = prev;
prev = cur;
cur = curNext;
}
return newHead;
}
public Node middleNode ( ) {
int len = Size ( ) / 2 ;
Node cur = this . head;
int count = 0 ;
while ( count!= len) {
cur = cur. next;
count++ ;
}
return cur;
}
public Node middleNodeplus ( ) {
Node slow = this . head;
Node fast = this . head;
while ( fast!= null && fast. next!= null) {
fast = fast. next. next;
slow = slow. next;
}
return slow;
}
}
测试代码
public class TestDemo {
public static void main ( String[ ] args) {
MyLinkedList myLinkedList = new MyLinkedList ( ) ;
myLinkedList. createLinked ( ) ;
myLinkedList. display ( ) ;
System. out. println ( "=================" ) ;
Node cur = myLinkedList. findLastNode ( ) ;
System. out. println ( cur. val) ;
System. out. println ( "=================" ) ;
System. out. println ( myLinkedList. Size ( ) ) ;
System. out. println ( "=================" ) ;
System. out. println ( myLinkedList. contains ( 42 ) ) ;
myLinkedList. addLast ( 3 ) ;
myLinkedList. display ( ) ;
System. out. println ( "================" ) ;
myLinkedList. addIndex ( 2 , 21 ) ;
myLinkedList. display ( ) ;
System. out. println ( "================" ) ;
myLinkedList. remove ( 22 ) ;
myLinkedList. display ( ) ;
Node ret = myLinkedList. middleNodeplus ( ) ;
System. out. println ( ret. val) ;
}
}