一、計算鏈表中節點個數
/**
* @param head 不計算頭結點
* @return count 鏈表中的結點個數
*/
public static int getLength(Node head) {
if(head.next==null) {
return 0;
}
Node temp =head.next;
int count=0;
while(temp!=null){
count++;
temp=temp.next;
}
return count;
}
二、查找倒數第k個節點
/**
* @param k
* @param head
* @return 回傳倒數第k個節點
*/
public static Node findIndexNode(int k,Node head){
if(head.next == null){
System.out.println("鏈表為空");
return:
}
int size = getLength(head);
int num = size-k; //要遍歷的次數
Node temp = head.next;
for(int i =0;i<num;i++){
temp=temp.next;
}
return temp;
}
三、單鏈表的反轉
public static void reverse(Node head) {
if(head.next ==null||head.next.next==null){
return;
}
Node cur=head.next;
Node next=null;
Node newList=new Node(0);
while(cur!=null){
next=cur.next; //next保存下一節點
cur.next =newList.next; //將cur的下一節點加入新鏈表的最前端
newList.next=cur; //當前節點鏈接到新鏈表
cur=next; //cur指向下一節點
}
head.next=newList.next;
}
四、從尾到頭列印單鏈表
public static void reversePrint(Node head) {
Stack<Node> stack = new Stack();
Node temp = head.next;
//將所有節點壓入堆疊
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
//當堆疊不為空時,彈堆疊并顯示
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
五、洗掉排序鏈表中的重復元素(不帶頭結點)
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
public ListNode deleteDuplicates(ListNode head) {
if(head == null){
return null;
}
ListNode node = head;
while(node!=null&&node.next!=null){
if(node.val==node.next.val){
node.next= node.next.next;
}
else{
node=node.next;
}
}
return head;
}
六、判斷鏈表是否有環(不帶頭結點)

快指標比慢指標每次走兩步
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
七、回傳環形鏈表中的第一個結點(不帶頭結點)
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
public ListNode detectCycle(ListNode head) {
if( head == null || head.next ==null){
return null;
}
ListNode slow =head;
ListNode fast= head;
while(fast!=null){
slow = slow.next;
if(fast.next != null){
fast=fast.next.next;
}else{
return null;
}
if(fast == slow){
ListNode temp = head;
while(temp!=slow){
temp=temp.next;
slow=slow.next;
}
return temp;
}
}
return null;
八、兩兩交換鏈表中的結點(不帶頭結點)


* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
public ListNode swapPairs(ListNode head) {
ListNode pre =new ListNode(0);
pre.next =head;
ListNode temp = pre;
while(temp.next!=null&&temp.next.next!=null){
ListNode node1 = temp.next;
ListNode node2=temp.next.next;
temp.next= node2; //交換前后結點
node1.next =node2.next;
node2.next =node1;
temp=node1;
}
return pre.next;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/174934.html
標籤:其他
下一篇:Java中常見的運算子
