剑指offer 06.24.35. 链表

题目一: 

剑指 Offer 06. 从尾到头打印链表https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

想法: 将链表倒序放入数组中 再返回数组

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode node=head;
        Stack<Integer> st= new Stack<>();
        int cnt=0;
        int i=0;
        if(node==null)
            return new int[0];
        //统计结点个数
        while(node.next!=null){
            node=node.next;
            cnt++;
        }      
        node=head;
        int[] print = new int[cnt+1];
        //将链表倒序放入数组中 
        for(i=cnt;i>=0;i--){
            print[i]=node.val;
            node=node.next;
        }
        return print;
    }
}

结果:

 题目二:

剑指 Offer 24. 反转链表https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/


想法: 新建一个头节点 反转链表

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        //新链表 设为空
        ListNode rev = null;
        //遍历旧链表
        while(head!=null){
            //提取每一个节点
            ListNode cur = new ListNode(head.val);
            //头插法插入
            cur.next=rev;
            rev=cur;
            //遍历下一个节点
            head=head.next;
        }
        return rev;
    }
}

结果:

题目三:

剑指 Offer 35. 复杂链表的复制https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/

想法:在每一个结点后重复此节点 再将奇数位置结点的random域给下一个结点 最终拆分原链表和结果链表即可

代码:

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/
class Solution {
    public Node copyRandomList(Node head) {
        if(head==null)
            return null;
        //辅助结点
        Node curr = head;
        //重复每一个结点 形成新链表
        //遍历每个结点 所以终止条件:curr!=null
        while(curr!=null){
            Node temp=new Node(curr.val);
            temp.next=curr.next;
            curr.next=temp;
            curr=curr.next.next; //或 curr=temp.next;
        }
        //给重复的结点 赋random域的值
        curr=head;
        //遍历每个(单数位置的)结点 所以终止条件:curr!=null
        while(curr!=null){
            //空的不用赋值就已经是空了
            if(curr.random!=null)
                curr.next.random=curr.random.next;
            curr=curr.next.next;
        }
        //拆分单双数两个链表
        //ori为原链表 因为【复制链表】要求不改变原链表
        Node ori=head;
        //res指向新链表(结果链表)的头
        Node res=head.next;
        curr=head.next;
        
        //遍历到最后一个结点就可以停了 所以终止条件:curr.next!=null
        while(curr.next!=null){
            ori.next=ori.next.next;
            curr.next=curr.next.next;
            ori=ori.next;
            curr=curr.next;
        }
        //最终不要忘记把原链表尾部置空
        ori.next=null;
        //返回结果链表
        return res;
        
    }
}

 结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值