码迷,mamicode.com
首页 > 其他好文 > 详细

92. Reverse Linked List II

时间:2019-06-05 00:08:04      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:lse   mamicode   alt   node   list   链接   ems   mic   info   

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

标签:lse   mamicode   alt   node   list   链接   ems   mic   info   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!