首页 > 学院 > 开发设计 > 正文

92. Reverse Linked List II

2019-11-11 05:11:14
字体:
来源:转载
供稿:网友

题意易懂,就是烦,要判断m是否是1,2刷想个更好的方法,最好是什么情况都通用的方法。

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode *p = head; ListNode *pb = head; ListNode *q = head; ListNode *qn = head; if(m == n) return head; if(m == 1){ q = q -> next; for(int i = 3; i <= n; ++ i) q = q -> next; qn = q -> next; ListNode *tp = p; ListNode *t; p = p -> next; while(p != qn){ t = p -> next; p -> next = tp; tp = p; p = t; } head -> next = qn; return q; } p = p -> next; q = q -> next; for(int i = 3; i <= m; ++ i){ p = p -> next; pb = pb -> next; } for(int i = 3; i <= n; ++ i) q = q -> next; qn = q -> next; ListNode *e = p; ListNode *b = q; ListNode *tp = p; ListNode *t; p = p -> next; while(p != qn){ t = p -> next; p -> next = tp; tp = p; p = t; } pb -> next = q; e -> next = qn; return head; }};
上一篇:Django文件上传机制详解

下一篇:kruskal

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表