剑指Offer-16
来源:互联网 发布:网络b类违规是什么意思 编辑:程序博客网 时间:2024/06/05 23:41
题目:
反转链表,实现一个函数,输入一个链表的头节点,反转该链表,输出反转后链表的头节点。
实现
//coding = javapublic class Solution16 { public Node reverseList(Node head){ if(head==null){ return null; } if(head.next==null){ return head; } Node reverse_head = null; Node cur_node = head; Node pre_node = null; Node next_node; while (cur_node!=null){ next_node = cur_node.next; if(next_node == null){ reverse_head = cur_node; } cur_node.next = pre_node; pre_node = cur_node; cur_node = next_node; } return reverse_head; } public static Node reverseList2(Node head){ if(head == null){ return null; } if(head.next ==null){ return head; } Stack<Node> temp = new Stack<>(); while (head!=null){ temp.add(head); head=head.next; } Node reverse_node = temp.pop(); Node cur_node = reverse_node; while (!temp.empty()){ cur_node.next=temp.pop(); cur_node=cur_node.next; } cur_node.next=null; return reverse_node; } public static void main(String[] args){ Node head = new Node(2); head.next = new Node(3); head.next.next = new Node(4); head.next.next.next = new Node(5); Solution16 s16 = new Solution16(); Node re_head = s16.reverseList(head); while (re_head!=null){ System.out.print(re_head.value); re_head = re_head.next; } }}
阅读全文
1 0
- 【16-20】剑指offer
- 剑指Offer-16
- 剑指offer题16
- 剑指offer面试题16
- 【面试题】剑指offer 16
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 解决php安装过程中, mcrypt.h not found 的报错问题
- 一个类的对象可以成为另一个类的数据成员
- cache.xml配置
- Swift类型转换
- 将byte[] 数据输出到页面展示图片
- 剑指Offer-16
- stylus之变量(Variables)
- sql查找被锁表名,释放表资源
- idea误删文件恢复方法
- HDU
- LeetCode 167. Two Sum II
- PyTorch学习系列(五)——自定义loss
- 怎样将 MySQL 迁移到 MariaDB 上
- Openvswitch手册(4): Mirror