92. Reverse Linked List II(链表部分反转)

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

 

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        def reverse(a,b):
            if a == b:
                return a
            pre = None
            cur = a
            while cur!=b:
                c_next = cur.next
                cur.next = pre
                pre = cur
                cur = c_next
            return pre
        
        fake = ListNode(-1,head)
        a = fake
        for i in range(left-1):
            a = a.next
        b = head
        for i in range(right-1):
            b = b.next
        b_next = b.next
        a.next = reverse(a.next,b.next)
        cur = fake
        while cur and cur.next:
            cur = cur.next
        cur.next = b_next
        return fake.next

 

 

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4         if(head == nullptr || head->next == nullptr) return head;
 5         ListNode* pre = nullptr;
 6         ListNode* cur = head;
 7         while(cur != nullptr) {
 8             ListNode* c_next = cur->next;
 9             cur->next = pre;
10             pre = cur;
11             cur = c_next;
12         }
13         return pre;
14     }
15     ListNode* reverseBetween(ListNode* head, int left, int right) {
16         if(head == nullptr || head->next == nullptr) return head;
17         ListNode* fakehead  = new ListNode(0);
18         fakehead->next = head;
19 
20         ListNode* pre = fakehead;
21         ListNode* cur = head;
22         ListNode* right_node = head;
23 
24         //遍历找到左节点 pre 跟右节点
25         int cnt = 1;
26         while(cur!=nullptr) {
27             if(cnt==left-1) {
28                 pre = cur;
29             }
30             if (cnt == right) {
31                 right_node = cur;
32             }
33             cur = cur->next;
34             cnt++;
35         }
36 
37         ListNode* left_node = pre->next;
38         ListNode* c_next = right_node->next;
39         right_node->next = nullptr;
40         pre->next = reverseList(left_node);
41         left_node->next = c_next;
42         return fakehead->next;
43     }
44 };

 

 

 

 

 1 class Solution {
 2    public ListNode reverseBetween(ListNode head, int m, int n) {
 3         if(head == null) return head;
 4         ListNode fakehead = new ListNode(0);
 5         fakehead.next = head;
 6         ListNode pre  = fakehead;
 7         
 8        
 9         for (int i = 0; i < m-1;i++) pre = pre.next;
10         
11         ListNode start = pre.next;
12         ListNode then = start.next;
13         
14         for(int i = 0 ;i < n -m ;i++){
15             start.next= then.next;
16             then.next = pre.next;
17             pre.next = then;
18             then=start.next;
19         }
20     return fakehead.next;
21     }
22 }

 

posted @ 2017-10-20 22:11  乐乐章  阅读(197)  评论(0编辑  收藏  举报