简体   繁体   中英

k reverse a linked list

Here, I want to reverse every k elements of the linked list recursively. For the linked list 3 →​ 4 →​ 5 →​ 2 →​ 6 →​ 1 →​ 9 for kReverse(3) becomes 5​ → 4→​ 3→​ 1→​ 6→​ 2→​ 9→ 1 I am getting a NullPointerException .

public static Node<Integer> kReverseLinkedList(Node<Integer> head, int k, int size) {
        if(size<k) {
            return ReverseLinkedList.reverseLinkedList(head);
        }
        Node<Integer> temp = head;

        int i=1;
        while(i<k) {
            temp = temp.next;
            i++;
        }

        Node<Integer> newHead = temp;
        temp.next=null;

        ReverseLinkedList.reverseLinkedList(head);

        Node<Integer> smallerHead = kReverseLinkedList(head.next, k, size-k);

        head.next = smallerHead;

        return newHead;

    }

On a cursory glance, you do not update your recursion to use newHead.

Also, you need to take care of breaking and making pointers. When you reserver the first 3 elements, they need to point to their previous one.

4.next = 3 and 5.next = 4 (You need to manage that in code)

One tip:

This would be much easier using a Stack of size k that reverses the required elements and pops once its full recursively.

I'm not sure if this is whatever you want? The size isn't needed,

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);
    }
}

Output:

3 4 5 2 6 1 9 
5 4 3 1 6 2 9 
  1. I don't think you need a loop in a recursive version.

  2. To make it universal add an offset.

  3. Size isn't needed.

Here a real recursive version with offset (untested):

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM