方法1:直接使用索引進行,有點類型冒泡排序,可是又不一樣,時間複雜度O(n^2)。算法
0-1-2-3-4ide
1-0-2-3-4函數
1-2-0-3-4this
1-2-3-0-4spa
1-2-3-4-0code
將head轉到tailblog
再次將新head轉到倒數第二個排序
方法2:直接使用遞歸進行,指向與尾部索引更改遞歸
package com.cnblogs.mufasa.demo1; public class Node { public int value; public Node next; public Node(int value){ this.value=value; } public void add(int value){ Node pre=this; while (pre.next!=null){ pre=pre.next; } pre.next=new Node(value); } public static Node reverseList(Node head){//1,利用Node索引解決問題【完成】 Node next=null; Node pre=null; while (head!=null){ next=head.next; head.next = pre; pre=head; head=next; } return pre; } public static Node reverseList2(Node head){//2,利用遞歸解決問題【完成】 if (head == null || head.next == null) { return head;//節點爲空或單節點 } //遞歸反轉子lian鏈表 Node newList = reverseList2(head.next); //第三張圖,本來:1-2-3-4-->2-1-nul 34 head.next.next = head; head.next = null; return newList; } } class client{ public static void main(String[] args) { Node root=new Node(1); for(int i=2;i<10;i++){ root.add(i); } Node reRoot=Node.reverseList(root); Node reRoot1=Node.reverseList2(root); } }
給定一個單鏈表的頭節點 head,實現一個調整單鏈表的函數,使得每K個節點之間爲一組進行逆序,而且從鏈表的尾部開始組起,頭部剩餘節點數量不夠一組的不須要逆序。(不能使用隊列或者棧做爲輔助)索引
先來個簡單的算法【從頭開始每隔3個進行翻轉】:【遞歸調用】
public Node reverseForwordSingle(Node head,int k){//正序特定長度翻轉 if(head==null){ return null; }else if(head.next==null){ return head; } Node pre=null; Node next=null; int counter=0; while (head!=null &&counter<k){ next=head.next; head.next=pre; pre=head; head=next; counter+=1; } head=pre; while (pre.next!=null){ pre=pre.next; } pre.next=reverseForwordSingle(next,k); return head; }
後面的逆序翻轉,有使用順序翻轉的方法:
public Node reverseBackwordSingle(Node head,int k){//逆序特定長度翻轉 if(head==null){ return null; }else if(head.next==null){ return head; } int counter=1;//對數據進行計數 Node pre=head; while (pre.next!=null){ pre=pre.next; counter+=1; } if(counter%k==0){//恰好整除 return reverseForwordSingle(head,k); }else if(counter%k==1) {//前面須要進行 有1個餘數 pre = head; }else { pre = head; int counter1=1; while (counter1<counter%k){ counter1+=1; pre=pre.next; if(pre==null){ return head; } } } pre.next=reverseForwordSingle(pre.next,k); return head; }
package com.cnblogs.mufasa.demo2; public class Node { public int value; public Node next; public Node(int value){ this.value=value; } public void add(int value){//添加節點 Node pre=this; while (pre.next!=null){ pre=pre.next; } pre.next=new Node(value); } public static String traverse(Node root){ if(root==null){ return "null"; }else if(root.next==null){ return root.value+"-"; } String str=root.value+"-"; while (root.next!=null){ root=root.next; str+=root.value+"-"; } return str; } public Node reverseForwordSingle(Node head,int k){//正序特定長度翻轉 if(head==null){ return null; }else if(head.next==null){ return head; } Node pre=null; Node next=null; int counter=0; while (head!=null &&counter<k){ next=head.next; head.next=pre; pre=head; head=next; counter+=1; } head=pre; while (pre.next!=null){ pre=pre.next; } pre.next=reverseForwordSingle(next,k); return head; } public Node reverseBackwordSingle(Node head,int k){//逆序特定長度翻轉 if(head==null){ return null; }else if(head.next==null){ return head; } int counter=1;//對數據進行計數 Node pre=head; while (pre.next!=null){ pre=pre.next; counter+=1; } if(counter%k==0){//恰好整除 return reverseForwordSingle(head,k); }else if(counter%k==1) {//前面須要進行 有1個餘數 pre = head; }else { pre = head; int counter1=1; while (counter1<counter%k){ counter1+=1; pre=pre.next; if(pre==null){ return head; } } } pre.next=reverseForwordSingle(pre.next,k); return head; } } class Client{ public static void main(String[] args) { Node root=new Node(1); for(int i=2;i<10;i++){ root.add(i); } System.out.println(root.traverse(root)); // Node reRoot= root.reverseForwordSingle(root,3); // System.out.println(root.traverse(reRoot)); Node reRoot1= root.reverseBackwordSingle(root,5); System.out.println(root.traverse(reRoot1)); } } /* 1-2-3-4-5-6-7-8-9- 1-2-3-4-9-8-7-6-5- */