92. Reverse Linked List II
Posted wangyufeiaichiyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了92. Reverse Linked List II相关的知识,希望对你有一定的参考价值。
题目链接:https://leetcode.com/problems/reverse-linked-list-ii/
解题思路:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode 4 * int val; 5 * ListNode next; 6 * ListNode(int x) val = x; 7 * 8 */ 9 class Solution 10 public ListNode reverseBetween(ListNode head, int m, int n) 11 12 13 ListNode newhead = new ListNode(-1); 14 newhead.next = head; 15 16 if(head==null || head.next ==null) 17 return head; 18 19 ListNode startpoint = newhead; 20 ListNode node1 = null; 21 ListNode node2 = null; 22 23 for(int i=0;i<n;i++) 24 25 if(i<m-1) 26 27 startpoint = startpoint.next; 28 29 else if(i==m-1) 30 31 node1 = startpoint.next; 32 node2 = node1.next; 33 34 else 35 36 node1.next = node2.next; 37 node2.next = startpoint.next; 38 startpoint.next = node2; 39 node2 = node1.next; 40 41 42 return newhead.next; 43 44 45
以上是关于92. Reverse Linked List II的主要内容,如果未能解决你的问题,请参考以下文章
#Leetcode# 92. Reverse Linked List II