链表篇-链表翻转

链表翻转

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode *pre=nullptr;
        ListNode *cur=pHead;
        ListNode *nex=nullptr;
        while(cur)
        {
            nex=cur->next;
            cur->next=pre;
            pre=cur;
            cur=nex;
        
        }
        return pre;
        

    }
};

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

最普通的方法就是:

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

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

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

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

链表内指定区间翻转

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param m int整型 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here

        ListNode dump = new ListNode(-1);
        dump.next = head;
        ListNode left = dump;
        ListNode right = dump;
        for (int i = 0; i < m-1 ; i++) {
            left = left.next;
        }
        for (int i = 0; i < n; i++)
        {
            right = right.next;
        }
        ListNode newhead = left.next;
        left.next = null;
        
        ListNode oldright = right.next;
        right.next=null;
        
        rever(newhead,n-m);
        
        newhead.next=oldright;
        left.next=right;
        return dump.next;
    }
    public void rever(ListNode head,int num){
        ListNode cur = head;
        ListNode pre = null;
        for(int i=0;i<num+1;i++){
            ListNode next = cur.next;
            cur.next=pre;
            pre = cur;
            cur = next;
        }
    }
}

解决方法:

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

 链表内每k个一组翻转

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
        // write code here
        ListNode dump = new ListNode(-1);
        dump.next = head;
        ListNode end = dump;
        ListNode pre = dump;
        
        while(end.next!=null){
            for(int i=0;i<k&&end!=null;i++){
                end=end.next;
            }
            if(end==null){
                break;
            }
            ListNode start = pre.next;
            ListNode next = end.next;
            
            end.next=null;
            
            pre.next = rever(start);
            start.next = next;
            pre = start;
            end = start;
            
        }
        return dump.next;
        
    }
    public ListNode rever(ListNode head){
        ListNode cur = head;
        ListNode pre = null;
        while(cur!=null){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}

 解决方法:

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

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智博的自留地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值