LeetCode Reverse Linked List II 反置链表2

题意:将指定的一段位置[m,n]的链表反置,返回链表头。

思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊。

  目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交给DFS解决,用个计数器来统计已经反置的个数即可。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* DFS(ListNode* t,ListNode* far, int num)
12     {
13         if(num==0)  return far;
14         ListNode* tmp=t->next;
15         t->next=far;
16         return DFS(tmp , t, num-1);
17     }
18
19     ListNode* reverseBetween(ListNode* head, int m, int n) {
20         if(m==n)    return head;
21         ListNode* t=head, *r=head;
22
23         for(int i=0; i<n; i++)  r=r->next;     //找到第n+1个
24         if(m==1)    return DFS(head, r, n-m+1);
25
26         for(int i=1; i<m-1; i++)  t=t->next;   //找到第m-1个
27         t->next=DFS(t->next, r, n-m+1);
28         return head;
29     }
30 };

AC代码

时间: 2024-10-13 12:27:44

LeetCode Reverse Linked List II 反置链表2的相关文章

Leetcode: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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

Leetcode:Reverse Linked List II 反转链表区间

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->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

leetcode - Reverse Linked List II

leetcode - 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol

LeetCode: Reverse Linked List II [092]

[题目] 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

(每日算法)LeetCode --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n sati

[LeetCode] 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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

LeetCode OJ:Reverse Linked List II(反转链表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->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

[Leetcode] Reverse linked list ii 反转链表

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2->3->4->5->NULL, m = 2 and n = 4, return1->4->3->2->5->NULL. Note:  Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt