java 鏈表相關操作

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. import java.util.HashMap;  
  2. import java.util.Scanner;  
  3. import java.util.Stack;  
  4.   
  5. /** 
  6.  *  
  7.  * @author kerryfish 
  8.  * 關於java中鏈表的操作 
  9.  * 1. 求單鏈表中結點的個數: getListLength  
  10.  * 2. 將單鏈表反轉: reverseList(遍歷),reverseListRec(遞歸)  
  11.  * 3. 查找單鏈表中的倒數第K個結點(k > 0): reGetKthNode  
  12.  * 4. 查找單鏈表的中間結點: getMiddleNode  
  13.  * 5. 從尾到頭打印單鏈表: reversePrintListStack,reversePrintListRec(遞歸)  
  14.  * 6. 已知兩個單鏈表pHead1 和pHead2 各自有序,把它們合併成一個鏈表依然有序: mergeSortedList, mergeSortedListRec  
  15.  * 7. 對單鏈表進行排序,listSort(歸併),insertionSortList(插入) 
  16.  * 8. 判斷一個單鏈表中是否有環: hasCycle  
  17.  * 9. 判斷兩個單鏈表是否相交: isIntersect  
  18.  * 10. 已知一個單鏈表中存在環,求進入環中的第一個節點: getFirstNodeInCycle, getFirstNodeInCycleHashMap  
  19.  * 11. 給出一單鏈表頭指針head和一節點指針delete,O(1)時間複雜度刪除節點delete: deleteNode 
  20.  */  
  21. public class LinkedListSummary {  
  22.     /** 
  23.      * @param args 
  24.      *  
  25.      */  
  26.     public static class Node{  
  27.         int value;  
  28.         Node next;  
  29.         public Node(int n){  
  30.             this.value=n;  
  31.             this.next=null;  
  32.         }  
  33.     }  
  34.     public static void main(String[] args) {  
  35.         // TODO Auto-generated method stub  
  36.         Scanner in=new Scanner(System.in);  
  37.         Node head=null;  
  38.         if(in.hasNextInt()){  
  39.             head=new Node(in.nextInt());  
  40.         }  
  41.         Node temp=head;  
  42.         while(in.hasNextInt()){  
  43.             temp.next=new Node(in.nextInt());  
  44.             temp=temp.next;  
  45.         }  
  46.         in.close();  
  47.         //int len=getListLength(head);  
  48.         //Node reHead=reverseList(head);  
  49.         //reHead=reverseListRec(reHead);  
  50.         //Node node_k=reGetKthNode(head,3);  
  51.         //Node mid=getMiddleNode(head);  
  52.         //reversePrintListRec(head);  
  53.         //reversePrintListStack(head);  
  54.         //Node mergeHead=mergeSortedList(head,null);  
  55.         //Node sortHead=listSort(head);  
  56.           
  57.     }  
  58.     //求單鏈表中結點的個數: getListLength   
  59.     public static int getListLength(Node head){  
  60.         int len=0;  
  61.         while(head!=null){  
  62.             len++;  
  63.             head=head.next;  
  64.         }  
  65.         return len;  
  66.     }  
  67.     //將單鏈表反轉,循環  
  68.     public static Node reverseList(Node head){  
  69.         if(head==null||head.next==null)return head;  
  70.         Node pre=null;  
  71.         Node nex=null;  
  72.         while(head!=null){  
  73.             nex=head.next;  
  74.             head.next=pre;  
  75.             pre=head;  
  76.             head=nex;  
  77.         }  
  78.         return pre;  
  79.     }  
  80.     //將單鏈表反轉,遞歸  
  81.     public static Node reverseListRec(Node head){  
  82.         if(head==null||head.next==null)return head;  
  83.         Node reHead=reverseListRec(head.next);  
  84.         head.next.next=head;  
  85.         head.next=null;  
  86.         return reHead;  
  87.     }  
  88.     //查找單鏈表中的倒數第K個結點(k > 0)  
  89.     public static Node reGetKthNode(Node head,int k){  
  90.         if(head==null)return head;  
  91.         int len=getListLength(head);  
  92.         if(k>len)return null;  
  93.         Node target=head;  
  94.         Node nexk=head;  
  95.         for(int i=0;i<k;i++){  
  96.             nexk=nexk.next;  
  97.         }  
  98.         while(nexk!=null){  
  99.             target=target.next;  
  100.             nexk=nexk.next;  
  101.         }  
  102.         return target;  
  103.     }  
  104.     //查找單鏈表的中間結點   
  105.     public static Node getMiddleNode(Node head){  
  106.         if(head==null||head.next==null)return head;  
  107.         Node target=head;  
  108.         Node temp=head;  
  109.         while(temp!=null&&temp.next!=null){  
  110.             target=target.next;  
  111.             temp=temp.next.next;  
  112.         }  
  113.         return target;  
  114.     }  
  115.     //從尾到頭打印單鏈表,遞歸  
  116.     public static void reversePrintListRec(Node head){  
  117.         if(head==null)return;  
  118.         else{  
  119.             reversePrintListRec(head.next);  
  120.             System.out.println(head.value);  
  121.         }  
  122.     }  
  123.     //從尾到頭打印單鏈表,棧  
  124.     public static void reversePrintListStack(Node head){  
  125.         Stack<Node> s=new Stack<Node>();  
  126.         while(head!=null){  
  127.             s.push(head);  
  128.             head=head.next;  
  129.         }  
  130.         while(!s.isEmpty()){  
  131.             System.out.println(s.pop().value);  
  132.         }  
  133.     }  
  134.     //合併兩個有序的單鏈表head1和head2,循環  
  135.     public static Node mergeSortedList(Node head1,Node head2){  
  136.         if(head1==null)return head2;  
  137.         if(head2==null)return head1;  
  138.         Node target=null;  
  139.         if(head1.value>head2.value){  
  140.             target=head2;  
  141.             head2=head2.next;  
  142.         }  
  143.         else{  
  144.             target=head1;  
  145.             head1=head1.next;  
  146.         }  
  147.         target.next=null;  
  148.         Node mergeHead=target;  
  149.         while(head1!=null && head2!=null){  
  150.             if(head1.value>head2.value){  
  151.                 target.next=head2;  
  152.                 head2=head2.next;  
  153.             }  
  154.             else{  
  155.                 target.next=head1;  
  156.                 head1=head1.next;  
  157.             }  
  158.             target=target.next;  
  159.             target.next=null;  
  160.         }  
  161.         if(head1==null)target.next=head2;  
  162.         else target.next=head1;  
  163.         return mergeHead;  
  164.     }  
  165.     //合併兩個有序的單鏈表head1和head2,遞歸  
  166.     public static Node mergeSortedListRec(Node head1,Node head2){  
  167.         if(head1==null)return head2;  
  168.         if(head2==null)return head1;  
  169.         if(head1.value>head2.value){  
  170.             head2.next=mergeSortedListRec(head2.next,head1);  
  171.             return head2;  
  172.         }  
  173.         else{  
  174.             head1.next=mergeSortedListRec(head1.next,head2);  
  175.             return head1;  
  176.         }  
  177.     }  
  178.     //對單鏈表進行排序,歸併排序,在排序裏面不建議選用遞歸的合併有序鏈表算法,如果鏈表長度較長,很容易出現棧溢出  
  179.     public static Node listSort(Node head){  
  180.         Node nex=null;  
  181.         if(head==null||head.next==null)return head;  
  182.         else if(head.next.next==null){  
  183.             nex=head.next;  
  184.             head.next=null;  
  185.         }  
  186.         else{  
  187.             Node mid=getMiddleNode(head);  
  188.             nex=mid.next;  
  189.             mid.next=null;  
  190.         }  
  191.         return mergeSortedList(listSort(head),listSort(nex));//合併兩個有序鏈表,不建議遞歸  
  192.     }  
  193.     //對單鏈表進行排序,插入排序  
  194.     public Node insertionSortList(Node head) {  
  195.         if(head==null||head.next==null)return head;  
  196.         Node pnex=head.next;  
  197.         Node pnex_nex=null;  
  198.         head.next=null;  
  199.         while(pnex!=null){  
  200.             pnex_nex=pnex.next;  
  201.             Node temp=head;  
  202.             Node temp_pre=null;  
  203.             while(temp!=null){  
  204.                 if(temp.value>pnex.value)break;  
  205.                 temp_pre=temp;  
  206.                 temp=temp.next;  
  207.             }  
  208.             if(temp_pre==null){  
  209.                 head=pnex;  
  210.                 pnex.next=temp;  
  211.             }  
  212.             else{  
  213.                 temp_pre.next=pnex;  
  214.                 pnex.next=temp;  
  215.             }  
  216.             pnex=pnex_nex;  
  217.         }  
  218.         return head;  
  219.     }  
  220.     //判斷一個單鏈表中是否有環,快慢指針  
  221.     public static boolean hasCycle(Node head){  
  222.         boolean flag=false;  
  223.         Node p1=head;  
  224.         Node p2=head;  
  225.         while(p1!=null&&p2!=null){  
  226.             p1=p1.next;  
  227.             p2=p2.next.next;  
  228.             if(p2==p1){  
  229.                 flag=true;  
  230.                 break;  
  231.             }  
  232.         }  
  233.         return flag;  
  234.     }  
  235.     //判斷兩個單鏈表是否相交,如果相交返回第一個節點,否則返回null  
  236.     //如果單純的判斷是否相交,只需要看最後一個指針是否相等  
  237.     public static Node isIntersect(Node head1,Node head2){  
  238.         Node target=null;  
  239.         if(head1==null||head2==null)return target;  
  240.         int len1=getListLength(head1);  
  241.         int len2=getListLength(head2);  
  242.         if(len1>=len2){  
  243.             for(int i=0;i<len1-len2;i++){  
  244.                 head1=head1.next;  
  245.             }  
  246.         }else{  
  247.             for(int i=0;i<len2-len1;i++){  
  248.                 head2=head2.next;  
  249.             }  
  250.         }  
  251.         while(head1!=null&&head2!=null){  
  252.             if(head1==head2){  
  253.                 target=head1;  
  254.                 break;  
  255.             }  
  256.             else{  
  257.                 head1=head1.next;  
  258.                 head2=head2.next;  
  259.             }  
  260.         }  
  261.         return target;  
  262.     }  
  263.     //已知一個單鏈表中存在環,求進入環中的第一個節點,利用hashmap,不要用ArrayList,因爲判斷ArrayList是否包含某個元素的效率不高  
  264.     public static Node getFirstNodeInCycleHashMap(Node head){  
  265.         Node target=null;  
  266.         HashMap<Node,Boolean> map=new HashMap<Node,Boolean>();  
  267.         while(head!=null){  
  268.             if(map.containsKey(head))target=head;  
  269.             else{  
  270.                 map.put(head, true);  
  271.             }  
  272.             head=head.next;  
  273.         }  
  274.         return target;  
  275.     }  
  276.     //已知一個單鏈表中存在環,求進入環中的第一個節點,不用hashmap  
  277.     //用快慢指針,與判斷一個單鏈表中是否有環一樣,找到快慢指針第一次相交的節點,此時這個節點距離環開始節點的長度和鏈表投距離環開始的節點的長度相等  
  278.     public static Node getFirstNodeInCycle(Node head){  
  279.         Node fast=head;  
  280.         Node slow=head;  
  281.         while(fast!=null&&fast.next!=null){  
  282.             slow=slow.next;  
  283.             fast=fast.next.next;  
  284.             if(slow==fast)break;  
  285.         }  
  286.         if(fast==null||fast.next==null)return null;//判斷是否包含環  
  287.         //相遇節點距離環開始節點的長度和鏈表投距離環開始的節點的長度相等  
  288.         slow=head;  
  289.         while(slow!=fast){  
  290.             slow=slow.next;  
  291.             fast=fast.next;  
  292.         }//同步走  
  293.         return slow;  
  294.           
  295.     }  
  296.     //給出一單鏈表頭指針head和一節點指針delete,O(1)時間複雜度刪除節點delete  
  297.     //可惜採用將delete節點value值與它下個節點的值互換的方法,但是如果delete是最後一個節點,則不行,但是總得複雜度還是O(1)  
  298.     public static void deleteNode(Node head,Node delete){  
  299.         //首先處理delete節點爲最後一個節點的情況  
  300.         if(delete==null)return;  
  301.         if(delete.next==null){  
  302.             if(head==delete)head=null;  
  303.             else{  
  304.                 Node temp=head;  
  305.                 while(temp.next!=delete){  
  306.                     temp=temp.next;  
  307.                 }  
  308.                 temp.next=null;  
  309.             }  
  310.         }  
  311.         else{  
  312.             delete.value=delete.next.value;  
  313.             delete.next=delete.next.next;  
  314.         }  
  315.         return;  
  316.     }  
  317. }  

發佈了12 篇原創文章 · 獲贊 7 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章