[英]k reverse a linked list
Here, I want to reverse every k elements of the linked list recursively. 在这里,我想递归地反转链表中的每k个元素。 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
. 对于链接列表3 → 4 → 5 → 2 → 6 → 1 → 9
对于kReverse(3)
变为5 → 4→ 3→ 1→ 6→ 2→ 9→ 1
我收到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. 粗略地看,您不会将递归更新为使用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) 4.next = 3和5.next = 4(您需要在代码中进行管理)
One tip: 提示:
This would be much easier using a Stack of size k that reverses the required elements and pops once its full recursively. 使用大小为k的Stack可以更轻松,该Stack可以反转所需的元素,并在递归将其完全填充后弹出。
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
I don't think you need a loop in a recursive version. 我认为您不需要在递归版本中循环。
To make it universal add an offset. 要使其通用,请添加一个偏移量。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.