链表类算法题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Liuhe_5656/article/details/52460596

本文只作为学习笔记,如若侵权请告知,一定及时删除

题目(1)

在一个链表中,查找倒数的第k个数。

思路:

使用双指针的方式,前一个指针先走k步(中间隔k-1个结点),后一个指针才开始走,直到第一个指针走到尾,后一个指针指向的就是要找的倒数第k个数。值得注意的是:1、k是否超过链表长度且k必须为正整数;2、链表是否为空。

链表结构

class Node{

    private int value;
    private Node next;

    public Node(int value) {
        super();
        this.value = value;
    }

    public Node(int value, Node next) {
        super();
        this.value = value;
        this.next = next;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

}

public class Item1 {

    public static void main(String[] args) {

        Node head = new Node(1);
        Node h1 = new Node(2);
        Node h2 = new Node(3);
        Node h3 = new Node(4);
        Node h4 = new Node(5);

        head.setNext(h1);
        h1.setNext(h2);
        h2.setNext(h3);
        h3.setNext(h4);

        Node find = findNode(head, 4);
        System.out.println(find.getValue() + "");


    }

    private static Node findNode(Node head, int i) {

        if (head == null && i < 0) {
            return null;
        }

        Node prePoint = head;
        Node postPoint = head;

        for (int j = 0; j < i - 1; j++) {
            if (prePoint.getNext() != null) {
                prePoint = prePoint.getNext();
            }else {
                return null;
            }
        }

        while (prePoint.getNext() != null) {

            prePoint = prePoint.getNext();
            postPoint = postPoint.getNext();

        }

        return postPoint;

    }

}

题目(2)

 反转链表,对于一个链表,反转该链表并返回头结点。

思路

主要是指针的操作,但是要注意不能断链。这里可以使用非递归的方式求解。
class ListNode{

    private int value;
    private ListNode next;

    public ListNode(int value) {
        super();
        this.value = value;
    }

    public ListNode(int value, ListNode next) {
        super();
        this.value = value;
        this.next = next;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }

}

public class Item2 {

    public static void main(String[] args) {

        ListNode head = new ListNode(1);
        ListNode h1 = new ListNode(2);
        ListNode h2 = new ListNode(3);
        ListNode h3 = new ListNode(4);
        ListNode h4 = new ListNode(5);

        head.setNext(h1);
        h1.setNext(h2);
        h2.setNext(h3);
        h3.setNext(h4);

        ListNode receHead = reverseList(head);
        while(receHead != null){
            System.out.println(receHead.getValue());
            receHead = receHead.getNext();
        }

    }

    private static ListNode reverseList(ListNode head){

        ListNode reverHead = null;
        ListNode pNode = head;
        ListNode pPrev = null;

        while (pNode != null) {

            ListNode pNext = pNode.getNext();

            if (pNext == null) {
                reverHead = pNode;
            }

            pNode.setNext(pPrev);
            pPrev = pNode;
            pNode = pNext;
        }

        return reverHead;
    }

}

感谢

谢谢一个博主的分享,在此学习以作记录。
博主地址:http://my.csdn.net/google19890102

猜你喜欢

转载自blog.csdn.net/Liuhe_5656/article/details/52460596