首页 > 其他 > 详细

92. Reverse Linked List II

时间:2019-06-04 23:19:15      阅读:108      评论:0      收藏:0      [点我收藏+]

题目链接: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

原文:https://www.cnblogs.com/wangyufeiaichiyu/p/10976836.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!