单向链表基础:
public class ListNode {
private int data;
private ListNode next;
public int getData ( ) {
return data;
}
public ListNode getNext ( ) {
return next;
}
public void setNext ( ListNode next) {
this . next = next;
}
public void setData ( int data) {
this . data = data;
}
public int ListLength ( ListNode headNode) {
int length= 0 ;
ListNode currentNode= headNode;
while ( currentNode!= null ) {
length++ ;
currentNode = currentNode. next;
}
return length;
}
public ListNode insertListNode ( ListNode headNdoe, ListNode nodeToInsert, int position) {
if ( headNdoe== null ) {
return nodeToInsert;
}
int size= ListLength ( headNdoe) ;
if ( position> size+ 1 || position< 1 ) {
System . out. println ( "无效插入位" ) ;
return headNdoe;
}
if ( position== 1 ) {
nodeToInsert. setNext ( headNdoe) ;
return nodeToInsert;
} else {
ListNode preNode= headNdoe;
int count= 1 ;
while ( count< position- 1 ) {
preNode= preNode. getNext ( ) ;
count++ ;
}
ListNode nextNode = preNode. getNext ( ) ;
nodeToInsert. setNext ( nextNode) ;
preNode. setNext ( nodeToInsert) ;
}
return headNdoe;
}
public ListNode DeleteNode ( ListNode headNode, int position) {
int size= ListLength ( headNode) ;
if ( position > size|| position< 1 ) {
System . out. println ( "删除越界" ) ;
return headNode;
}
if ( position == 1 ) {
ListNode nextNode= headNode. getNext ( ) ;
headNode= null ;
return nextNode;
} else {
ListNode preNode= headNode;
int count= 1 ;
while ( count< position) {
preNode = preNode. getNext ( ) ;
count++ ;
}
ListNode nextNode= preNode. getNext ( ) ;
preNode. setNext ( nextNode. getNext ( ) ) ;
nextNode = null ;
}
return headNode;
}
public void DeleteListNode ( ListNode head) {
ListNode auxilaryNode, iterator= head;
while ( iterator != null ) {
auxilaryNode = iterator. getNext ( ) ;
iterator = null ;
iterator = auxilaryNode;
}
}
public void addListNode ( int val, ListNode head) {
while ( head. next!= null ) {
head= head. next;
}
ListNode addOne= new ListNode ( ) ;
addOne. data= val;
head. next= addOne;
}
public void addLotListNode ( int [ ] nums, ListNode head) {
while ( head. next!= null ) {
head= head. next;
}
ListNode addOne= new ListNode ( ) ;
head. next= addOne;
for ( int i= 0 ; i< nums. length; i++ ) {
addOne. data= nums[ i] ;
addOne. next= new ListNode ( ) ;
addOne= addOne. next;
}
}
public static void reverseListNode ( ListNode head) {
if ( head. next!= null ) {
reverseListNode ( head. next) ;
head. next. next = head;
System . out. println ( head. data) ;
}
head. next= null ;
}
}
单向链表有关问题
找到链表倒数第N个点 方法一:蛮力法,经过两次遍历。 方法二:散列表法,建立一个HashMap,存储值与节点,返回Length-n的key的节点 方法三:双指针,一个指针在另一个指针出发n格后出发,这样当前指针到头后,后指针指向N 判定节点是以NULL结尾,还是形成了一个环 方法一:定义一个Lsit<>放入ListNode,如果有null则无环,如果有list.contain则有环 方法二:Floyd环判定算法,利用两个快慢不一的指针,如果相遇则存在环
public boolean DoesLinkedLsitContainsLoop ( ListNode head) {
if ( head == null ) {
return false ;
}
ListNode slowPtr= head;
ListNode fastPtr= head;
while ( fastPtr. next!= null && fastPtr. next. next!= null ) {
slowPtr= slowPtr. next;
fastPtr= fastPtr. next. next;
if ( fastPtr== slowPtr) {
return true ;
}
}
return false ;
}
在有序链表中插入一个节点。
public ListNode InsertInSortedList ( ListNode head, ListNode newNode) {
ListNode current = head;
if ( head == null ) {
return newNode;
}
ListNode temp = current;
while ( current!= null && current. getData ( ) < newNode. getData ( ) ) {
temp = current;
current= current. next;
}
newNode. next= current;
temp. next= newNode;
return head;
}
逆置单向链表
public ListNode ReverseList ( ListNode head) {
ListNode temp= null ;
ListNode nextNode= null ;
while ( head!= null ) {
nextNode= head. getNext ( ) ;
head. next= temp;
temp= head;
head= nextNode;
}
return temp;
}