当前位置:   article > 正文

链表篇-链表翻转

链表翻转

链表翻转

  1. class Solution {
  2. public:
  3. ListNode* ReverseList(ListNode* pHead) {
  4. ListNode *pre=nullptr;
  5. ListNode *cur=pHead;
  6. ListNode *nex=nullptr;
  7. while(cur)
  8. {
  9. nex=cur->next;
  10. cur->next=pre;
  11. pre=cur;
  12. cur=nex;
  13. }
  14. return pre;
  15. }
  16. };

思考:翻转链表是最基本的链表操作,可以用栈来中转,然后一次弹出的结果;

最普通的方法就是:

1、记录当前节点、下一节点、上一节点

2、流程:下一节点指向cur.next

当前节点的下一节点指向上一节点

上一节点后移、当前节点后移

链表内指定区间翻转

  1. import java.util.*;
  2. /*
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next = null;
  6. * }
  7. */
  8. public class Solution {
  9. /**
  10. *
  11. * @param head ListNode类
  12. * @param m int整型
  13. * @param n int整型
  14. * @return ListNode类
  15. */
  16. public ListNode reverseBetween (ListNode head, int m, int n) {
  17. // write code here
  18. ListNode dump = new ListNode(-1);
  19. dump.next = head;
  20. ListNode left = dump;
  21. ListNode right = dump;
  22. for (int i = 0; i < m-1 ; i++) {
  23. left = left.next;
  24. }
  25. for (int i = 0; i < n; i++)
  26. {
  27. right = right.next;
  28. }
  29. ListNode newhead = left.next;
  30. left.next = null;
  31. ListNode oldright = right.next;
  32. right.next=null;
  33. rever(newhead,n-m);
  34. newhead.next=oldright;
  35. left.next=right;
  36. return dump.next;
  37. }
  38. public void rever(ListNode head,int num){
  39. ListNode cur = head;
  40. ListNode pre = null;
  41. for(int i=0;i<num+1;i++){
  42. ListNode next = cur.next;
  43. cur.next=pre;
  44. pre = cur;
  45. cur = next;
  46. }
  47. }
  48. }

解决方法:

将链表翻转作为函数,先找出中间需要翻转的位置,要记录好断开处的位置,然后将需要反转的链表头以及长度传入,反转之后在接入原来的链表

 链表内每k个一组翻转

  1. import java.util.*;
  2. /*
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next = null;
  6. * }
  7. */
  8. public class Solution {
  9. /**
  10. *
  11. * @param head ListNode类
  12. * @param k int整型
  13. * @return ListNode类
  14. */
  15. public ListNode reverseKGroup (ListNode head, int k) {
  16. // write code here
  17. ListNode dump = new ListNode(-1);
  18. dump.next = head;
  19. ListNode end = dump;
  20. ListNode pre = dump;
  21. while(end.next!=null){
  22. for(int i=0;i<k&&end!=null;i++){
  23. end=end.next;
  24. }
  25. if(end==null){
  26. break;
  27. }
  28. ListNode start = pre.next;
  29. ListNode next = end.next;
  30. end.next=null;
  31. pre.next = rever(start);
  32. start.next = next;
  33. pre = start;
  34. end = start;
  35. }
  36. return dump.next;
  37. }
  38. public ListNode rever(ListNode head){
  39. ListNode cur = head;
  40. ListNode pre = null;
  41. while(cur!=null){
  42. ListNode next = cur.next;
  43. cur.next = pre;
  44. pre = cur;
  45. cur = next;
  46. }
  47. return pre;
  48. }
  49. }

 解决方法:

外部的while循环控制链表的循环、里面的for循环控制k个为一组的循环,循环变量就是一个一个遍历链表节点,当够了k个尾一组就记下这一组的前一个节点,以及后一个节点,然后调用翻转函数,这个翻转函数和前面的不同,需要返回翻转后的头结点,链表翻转后的头是返回的新头,尾是翻转前传的头,这样将尾和之前记录的后一个节点连接回原链表继续遍历。

当然翻转链表这个功能同样可以用队列或者栈来实现。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/209574
推荐阅读
相关标签
  

闽ICP备14008679号