鏈表

題目描述

輸入一個鏈表,輸出該鏈表中倒數第k個結點。

代碼思路如下:兩個指針,先讓第一個結點和第二個結點都指向頭結點,然後再讓第一個結點走(k-1)步,到達第k個節點。然後兩個指針同時往後移動,當第一個結點到達末尾的時候,第二個結點所在位置就是倒數第k個節點了。相當於用兩個節點做長度爲K的一個尺子;

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null||k<=0){
            return null;
        }
        ListNode pre=head;
        ListNode last=head;       
        for(int i=1;i<k;i++){
            if(pre.next!=null){
                pre=pre.next;
            }else{
                return null;
            }
        }
        while(pre.next!=null){
            pre = pre.next;
            last=last.next;
        }
        return last;
    }
}

題目描述

輸入一個鏈表,反轉鏈表後,輸出新鏈表的表頭。

public class Solution {
    public ListNode ReverseList(ListNode head) {
       
        if(head==null)
            return null;
        //head爲當前節點,如果當前節點爲空的話,那就什麼也不做,直接返回null;
        ListNode current = head; 
        ListNode pre = null;
        ListNode tmp = null;
        //當前節點是current ,pre爲當前節點的前一節點,tmp爲當前節點的下一節點
        //需要pre和tmp的目的是讓當前節點從pre->current 變成pre<-current 
        //即pre讓節點可以反轉所指方向,但反轉之後如果不用tmp 節點保存current.next節點的話,此單鏈表就此斷開了
        //所以需要用到pre和tmp兩個節點
        //1->2->3->4->5
        //1<-2<-3 4->5
        while(current !=null){
            //做循環,如果當前節點不爲空的話,始終執行此循環,此循環的目的就是讓當前節點從指向current.next到指向pre
            //如此就可以做到反轉鏈表的效果
            //先用tmp保存current 的下一個節點的信息,保證單鏈表不會因爲失去current節點的原next節點而就此斷裂
            tmp = current.next;
            //保存完current.next,就可以讓head從指向current.next變成指向pre了,代碼如下
            current.next = pre;
            //current指向pre後,就繼續依次反轉下一個節點
            //讓pre,current,current.next依次向後移動一個節點,繼續下一次的指針反轉
            pre = current;
            current= tmp;
        }
        //如果current爲null的時候,pre就爲最後一個節點了,但是鏈表已經反轉完畢,pre就是反轉後鏈表的第一個節點
        //直接輸出pre就是我們想要得到的反轉後的鏈表
        return pre;
    }
}

 

題目描述

輸入兩個單調遞增的鏈表,輸出兩個鏈表合成後的鏈表,當然我們需要合成後的鏈表滿足單調不減規則。

遞歸版本:

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        if(list1.val<=list2.val){
            list1.next = Merge(list1.next,list2);
            return list1;
        }else{
            list2.next = Merge(list1,list2.next);
            return list2;
        }
    }
}

非遞歸版本:

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        ListNode current = null;
        ListNode newNode = null;
        while(list1!=null&&list2!=null){
            if(list1.val<=list2.val){
                if(current==null){
                    current = list1;
                    newNode = current;
                }else{
                    current.next = list1;
                    current = current.next;
                }
                list1 = list1.next;
            }else{
                if(current==null){
                    current = list2;
                    newNode = current;
                }else{
                    current.next = list2;
                    current = current.next;
                }
                list2 = list2.next;
            }
        }
        if(list1==null){
            current.next = list2;
        }
        if(list2==null){
            current.next = list1;
        }
        return newNode;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章