剑指offer 链表专题 刷题记录(上)

3、从尾到头打印链表 (栈+list)


/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(listNode==null){
            return list;
        }
        Stack<Integer> stack=new Stack<Integer>();
        while(listNode!=null){
            stack.push(listNode.val);
            listNode=listNode.next;
        }
        
        while(!stack.isEmpty()){
            list.add(stack.pop());
        }
        return list;
    }
}


14 、链表的倒数第K个节点 (快慢指针+k<链表length校验)


import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode FindKthToTail (ListNode pHead, int k) {
        // write code here
        if(pHead==null||k<0){
            return pHead;
        }
        ListNode cur=pHead;
        ListNode begin=pHead;
        for(int i=0;i<k;i++){  ///判断链表长度是否小于k
           if(cur==null){
               return null;
           }else{
               cur=cur.next;
           }
            
        }
        while(cur!=null){
            cur=cur.next;
            begin=begin.next;
        }

            return begin;
        
    }
}


15 、反转链表 (头插法,双指针法)

//头插法


/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null){
            return null;
        }
        
        ListNode newhead=new ListNode(-1);
        ListNode next=null;
        while(head!=null){
            next=head.next;
            head.next=newhead.next;
            newhead.next=head;
            head=next;
        }
        return newhead.next;
    }
}


//双指针法


/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null){
            return null;
        }
         
        //判断边界条件
        ListNode pre=null;
        ListNode cur=head;
        ListNode next=null;
         
        while(cur!=null){
            next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
}


16 、合并两个排序链表


/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null){
            return list2;
        }else if(list2==null){
            return list1;
        }
        
        ListNode root=new ListNode(-1);
        ListNode head=root;
        while(list1!=null&&list2!=null){
            if(list1.val>list2.val){
                head.next=list2;
                head=list2;
                list2=list2.next;
            }else{
                head.next=list1;
                head=list1;
                list1=list2.next;
            }
           
        }
        
        if(list1!=null){
            head.next=list1;
        }
        
        if(list2!=null){
            head.next=list2;
        }
        return root.next;
        
    }
}
上一篇:kafka可视化客户端工具(Kafka Tool)的基本使用


下一篇:JAVA高频面试题目集锦(5)