反转链表前N个节点
作者:互联网
反转链表前N个节点,并返回反转后的链表
ListNode结构如下
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
//后继节点
ListNode successor = null;
public ListNode reverseN(ListNode head, int n){
if (head == null || head.next == null) return head;
if (n == 1){
successor = head.next;
return head;
}
ListNode last = reverseN(head.next, n-1);
head.next.next = head;
head.next = successor;
return last;
}
}
标签:head,ListNode,反转,next,链表,null,节点,successor 来源: https://blog.csdn.net/qq_40660894/article/details/113476707