k反转链表

时间:2015-07-21 19:05:10

标签: java data-structures linked-list reverse

这里,我想以递归方式反转链表的每个k元素。对于链表 3 →​ 4 →​ 5 →​ 2 →​ 6 →​ 1 →​ 9的{​​{1}}变为kReverse(3) 我收到5​ → 4→​ 3→​ 1→​ 6→​ 2→​ 9→ 1

NullPointerException

3 个答案:

答案 0 :(得分:2)

粗略地看一下,您不会更新递归以使用newHead。

另外,你需要注意打破和制作指针。当你保留前3个元素时,他们需要指向前一个元素。

4.next = 3和5.next = 4(你需要在代码中管理它)

一个提示:

使用大小为k的堆栈可以更容易地反转所需的元素并在其完全递归时弹出。

答案 1 :(得分:0)

我不确定这是否是你想要的? 不需要大小,

public class Node<T> {

    private T item;
    private Node<T> next;

    public Node(T item, Node<T> next) {
        this.item = item;
        this.next = next;
    }

    public Node(T item) {
        this.item = item;
    }

    public Node<T> getNext() {
        return next;
    }

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

    public static Node<Integer> reverseLinkedList(Node<Integer> head) {
        if (head.next != null){
            Node<Integer> prev = head.next;
            Node<Integer> result = reverseLinkedList(prev);
            prev.next = head;
            return result;
        } else {
            return head;
        }
    }

    private static Node<Integer> kReverseLinkedList(Node<Integer> head, Node<Integer> cursor, int counter, int k) {
        Node<Integer> result;
        if (cursor.next == null){
            result = reverseLinkedList(head);
            head.next = null;
        } else if (counter > 0){
            result = kReverseLinkedList(head, cursor.next, counter-1, k);
        } else {
            Node<Integer> next = cursor.next;
            cursor.next = null;
            result = reverseLinkedList(head);
            head.next = kReverseLinkedList(next, next, k, k);
        }
        return result;
    }

    public static Node<Integer> kReverseLinkedList(Node<Integer> head, int k) {
        Node<Integer> result = null;
        if (head != null){
            result = head;
            if (k > 1) {
                result = kReverseLinkedList(head, head, k-1, k-1);
            }
        }
        return result;
    }

    public static void print(Node<Integer> n) {
        if (n != null){
            System.out.print(n.item+" ");
            print(n.next);
        } else {
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[] data = {3,4,5,2,6,1,9};
        Node<Integer> head = new Node<>(data[0]);
        Node<Integer> tail = head;
        for (int i = 1; i < data.length; i++) {
            Node<Integer> n = new Node<>(data[i]);
            tail.setNext(n);
            tail = n;
        }
        print(head);
        head = kReverseLinkedList(head, 3);
        print(head);
    }
}

输出:

3 4 5 2 6 1 9 
5 4 3 1 6 2 9 

答案 2 :(得分:0)

  1. 我认为你不需要在递归版本中使用循环。

  2. 要使其通用,请添加偏移。

  3. 不需要尺寸。

  4. 这是一个带偏移(未经测试)的真实递归版本:

    private static Node<Integer> tail;
    
    public static Node<Integer> kReverseLinkedList(Node<Integer> head, int k, int offset, Node<Integer> current) {
      if (offset > 1) {
        kReverseLinkedList(head.next, k, offset - 1, head.next);
        return head;
      } else if (offset == 1) {
        head.next = kReverseLinkedList(head.next, k, 0, head.next);
        return head;
      } else if (k == 1) {
        tail = current.next;
        return current;  
      } else {
        Node<Integer> n = kReverseLinkedList(head, k - 1, 0, current.next);
        current.next.next = current;
        if (current == head)
          head.next = tail;
        return n;
      }
    }
    
    // usage: kReverseLinkedList(myListHead, 8, 2, myListHead); (k >= 2, offset >= 0)