然后写第二个部分,从最外层开始,并且假设,剩余的部分传入递归函数返回的为已经反转的链表,然后做剩余的部分。递归函数就是要注意一个头一个尾,然后中间部分由递归完成就可以了。
//给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。https://leetcode-cn.com/problems/reverse-linked-list/ publicclassNum206_reverseLinkedList {//自己用的双指针法,还有一种递归法,递不出来publicListNode reverseList(ListNode head) {if(head ==null|| head.next==null) {returnhead;} Li...
最后,返回newHead.next即为反转后的链表。 import com.kaesar.leetcode.ListNode;import java.util.Stack;public classLeetCode_092{public static ListNodereverseBetween(ListNode head,int left,int right){if(head==null||head.next==null){returnhead;}// 声明一个新的头节点ListNode newHead=newListNode(-1)...