将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度
,空间复杂度
。
例如:
给出的链表为
,
,
返回
.
例如:
给出的链表为
返回
数据范围: 链表长度
,
,链表中每个节点的值满足
要求:时间复杂度
,空间复杂度
进阶:时间复杂度
,空间复杂度
{1,2,3,4,5},2,4{1,4,3,2,5}{5},1,1{5}
{1,2,3,4},1,4{4,3,2,1}public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode preStart = dummy;
ListNode start = head;
for (int i = 1; i < m; i ++ ) {
preStart = start;
start = start.next;
}
// reverse
for (int i = 0; i < n - m; i ++ ) {
ListNode temp = start.next;
start.next = temp.next;
temp.next = preStart.next;
preStart.next = temp;
}
return dummy.next;
}
}
class Solution:
def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
res = ListNode(-1)
res.next = head
pre = res
cur = head
for i in range(1, m):
pre = cur
cur = cur.next
for i in range(m, n):
temp = cur.next
cur.next = temp.next
temp.next = pre.next
pre.next = temp
return res.next好难想明白啊
//1.将原链表分为三部分,m之前,m到n直接,n之后,并切断其联系
//2.将m,n之间链表反转
//3.将三部分重新连接
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode* dummy = new ListNode(-1);
dummy->next = head;
ListNode* pNode = dummy;
ListNode* ftail = pNode,*rhead = NULL, *rtail = NULL, *res = NULL;
for (int i = 0; i < m - 1; i++)
ftail = ftail->next;
rhead = ftail->next;
ftail->next = NULL;
ListNode* prev = NULL, *pcur = rhead, *pnext = rhead->next;
for (int i = m; i <= n;i++) {
pcur->next = prev;
prev = pcur;
pcur = pnext;
pnext = pnext->next;
}
res = pcur;
ftail->next = prev;
rhead->next = res;
ListNode* phead = dummy->next;
dummy->next = NULL;
delete dummy;
return phead;
}
};
public ListNode reverseBetween (ListNode head, int m, int n) {
// write code here
ListNode dum = new ListNode(0);
dum.next = head;
ListNode pre = dum;
for(int i = 1; i < m; i++){
pre = pre.next; // 找到m的上一个节点
}
head = pre.next; // 从m的位置开始进行交换
ListNode next; // 用于暂存遍历节点的后继节点
for(int i = m; i < n; i++){
// 暂存遍历节点的下一个节点
next = head.next;
// 让当前节点指向 后继节点的后继节点
head.next = next.next;
// 让后继节点指向反转元素的首位
next.next = pre.next;
// 让m的上一个节点 指向 此后继节点
pre.next = next;
}
return dum.next;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
ListNode* reverseBetween(ListNode* head, int m, int n) {
// 时间复杂度O(N),空间复杂度O(1)
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *pre = dummy;
for (int i = 1; i < m; ++i) pre = pre->next;
ListNode *cur = pre->next, *nex;
for (int i = m; i < n; ++i) {
nex = cur->next;
cur->next = nex->next;
nex->next = pre->next;
pre->next = nex;
}
return dummy->next;
}
}; class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode L = ListNode(0);
L.next = head;
ListNode *p = &L;
ListNode *q = head;
for(int i=1;i<m;i++)
{
p = q;
q = q->next;
}
for(int i=0;i<n-m;i++)
{
ListNode *t = q->next;
q->next = t->next;
t->next = p->next;
p->next = t;
}
return L.next;
}
}; ListNode *reverseBetween(ListNode *head, int m, int n) {
if(head == NULL || head->next == NULL) return head;
ListNode *q = NULL, *p = head;
for (int i = 0; i < m - 1; i++)
{
q = p;
p = p->next;
}
// p此时指向第m个结点
ListNode *end = p;
ListNode *pPre = p, *nxt = NULL;
p = p->next;
// m->n之间的结点逆序
for (int i = m + 1; i <= n; ++i)
{
nxt = p->next;
p->next = pPre;
pPre = p;
p = nxt;
}
// p指向原链表中第n个结点的下一个结点
// end表示逆序子链表的尾结点
end->next = p;
// pPre指向的是逆序后的子链表头
// q指向的是第m个结点前一个结点 注意有可能是NULL
if (q)
q->next = pPre; // 不是NULL 链接起来即可
else
head = pPre; // 是NULL 头结点即为子链表头
return head;
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head==null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy;
for(int i=1;i<m;i++) {
p = p.next;
}
ListNode pm = p.next;
for(int i=m;i<n;i++) {
ListNode temp = pm.next;
pm.next = temp.next;
temp.next = p.next;
p.next = temp;
}
return dummy.next;
}
}
class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here newHead = ListNode(0) # 附加一个头结点,使得空链表和非空链表得到统一 newHead.next = head p = newHead # 反转部分的前一个指针,可以看做是反转部分的头结点 cur = p.next i = 1 while i < n: if i < m: p = cur cur = p.next else: # 永远想着把当前结点的后继变成反转部分的前驱,当前结点永远是反转部分的最后一个结点 pre = cur.next cur.next = pre.next pre.next = p.next p.next = pre i += 1 return newHead.next
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode dummy(0);
dummy.next = head;
ListNode* p = &dummy;
for (int i = 0; i < m - 1; ++i)
p = p->next;
stack<ListNode*> stk;
ListNode* q = p->next;
for (int i = 0; i < n - m + 1; ++i) {
stk.emplace(q);
q = q->next;
}
while (not stk.empty()) {
p = p->next = stk.top();
stk.pop();
}
p->next = q;
return dummy.next;
}
}; struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
struct ListNode *newnode = malloc(sizeof(struct ListNode));
newnode->next=head;
struct ListNode* cur=newnode,*prev=newnode;
for(int i=0;i<m;i++)
{
prev=cur;
cur=cur->next;
}
for(int i=0;i<n-m;i++)
{
struct ListNode* next=cur->next;
cur->next=next->next;
next->next=prev->next;
prev->next=next;
}
return newnode->next;
} public ListNode reverseBetween (ListNode head, int m, int n) {
// write code here
ListNode pre=new ListNode(0) ,p1=pre;
pre.next=head;
for(int i=1;i<m;i++){
p1=p1.next;
}
ListNode p2=p1.next ,temp=p1.next;
for(int i=m;i<=n;i++){
ListNode node=p2.next;
p2.next=p1.next;
p1.next=p2;
p2=node;
}
temp.next=p2;
return pre.next;
} class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: # write code here temp = head res = [] length = 0 while temp is not None: length += 1 res.append(temp.val) temp = temp.next res1 = [] if n<length: res1 = res[:m-1]+res[m-1:n][::-1]+res[n:] else: res1 = res[:m-1]+res[m-1:n][::-1] temp = head i = 0 while temp is not None: temp.val = res1[i] temp = temp.next i += 1 return head
// 用一个列表添加head数据,用一个列表添加反转数据
// 然后把反转数据一一赋值到第一个列表上 就OK了
public ListNode reverseBetween (ListNode head, int m, int n) {
if(m==n) return head;
ArrayList<Integer> list=new ArrayList<>();
ArrayList<Integer> list1=new ArrayList<>();
ListNode current=head;
while (current!=null){
list.add(current.val);
current=current.next;
}
for (int i = m-1; i < n; i++) {
list1.add(list.get(i));
}
Collections.reverse(list1);
int index=0;
for (int i = m-1; i < n; i++) {
list.set(i,list1.get(index++));
}
current=head;
int i=0;
while (current!=null){
current.val=list.get(i);
i++;
current=current.next;
}
return head;
} /**
* Step1: 先创建一个“哨兵”节点,再找到索引m的前驱节点p
* Step2: 将m~n之间的节点采用头插法,插入到索引m的前驱节点后面
* Step3: 剩余的节点插入到头插链表的尾部
*/
ListNode *reverseBetween(ListNode *head, int m, int n) {
if (head == nullptr)
return head;
ListNode *p = new ListNode(-1), *root = p, *item, *q;
p->next = head;
int x = m;
while (--x > 0){
p = p->next;
}
bool first = true;
head = p->next;
p->next = nullptr;
while (m++ <= n){
item = head;
if (first){
q = item;
first = false;
}
head = head->next;
item->next = p->next;
p->next = item;
}
if (q != nullptr)
q->next = head;
return root->next;
}