Simple problem. If you know how to reverse a linked list, this problem will not be very hard.
Solution:
# T:O(n) S:O(1) class Solution: # @param head, a ListNode # @param m, an integer # @param n, an integer # @return a ListNode def reverseBetween(self, head, m, n): if head == None or head.next == None: return head dummy = ListNode(0) dummy.next = head head1 = dummy for i in range(m - 1): head1 = head1.next p = head1.next for i in range(n - m): tmp = head1.next head1.next = p.next p.next = p.next.next head1.next.next = tmp return dummy.nextRun Time: 64 ms
No comments:
Post a Comment