标签:
题目描述:/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode ReverseBetween(ListNode head, int m, int n) { var stack = new Stack<int>(); var q = new Queue<int>(); ListNode node = null; var c = 1; ListNode newNode = null; while(head != null) { if(c >= m && c <=n){ while(q.Count > 0){ var first = MoveNext(ref node , q.Dequeue()); if(first){ newNode = node; } } while(c >= m && c<= n){ stack.Push(head.val); head = head.next; c++; } if(head == null){ while(stack.Count > 0){ var first = MoveNext(ref node , stack.Pop()); if(first){ newNode = node; } } } } else{ while(stack.Count > 0){ var first = MoveNext(ref node , stack.Pop()); if(first){ newNode = node; } } var f = MoveNext(ref node , head.val); if(f) { newNode = node; } head = head.next; c++; } } return newNode; } private bool MoveNext(ref ListNode n , int val){ if(n == null){ n = new ListNode(val); return true; } else{ n.next = new ListNode(val); n = n.next; return false; } } }
LeetCode-- Reverse Linked List II
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/50144827