关于链表的反转

  • 引用

LeetCode 206.反转链表

  • 正文
    • 递归实现(运行77ms解决,效率极差)
      使用分治法的思想,拿到一个链表1-2-3-4-5,当你要反转这个链表的时候,你只需要得到2-3-4-5的反转,再加上1就可以了,同理2-3-4-5的反转就是将2-3-4反转,再结尾加上2便可。以此类推直到链表只剩下5,那5的反转就是自己本身再依次递归回去。
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null) return null;
            if (head.next == null) return head;
            else {
                ListNode node = reverseList(head.next);
                ListNode t = node;
                while (t.next != null) t = t.next;
                t.next = head;
                head.next = null;
                return node;
            }
        }
    }
    
    • 使用栈(运行4ms,效率差)
      使用栈暂存所有节点然后依次重新连接。
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null) return null;
            Stack<ListNode> stack = new Stack<>();
            ListNode t = head;
            while (t != null) {
                stack.push(t);
                t = t.next;
            }
            ListNode first = stack.pop();
            first.next = null;
            ListNode p = new ListNode(first.val);
            head = p;
            
            while (!stack.empty()) {
                ListNode a = stack.pop();
                a.next = null;
                p.next = a;
                p = p.next;
            }
            
            return head;
        }
    }
    
    • 迭代(运行不足1ms,效率高)
      从链表的首个位置取出元素指向新的链表,直到原链表不再拥有元素。
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    *     int val;
    *     ListNode next;
    *     ListNode(int x) { val = x; }
    * }
    */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null) return null;
            ListNode t = head;
            ListNode newList = null;
            ListNode p = head;
            while (t != null) {
                p = t.next;
                t.next = newList;
                newList = t;
                t = p;
            }
            return newList;
        }
    }
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 什么是数组? 数组简单来说就是将所有的数据排成一排存放在系统分配的一个内存块上,通过使用特定元素的索引作为数组的下...
    启明_b56f阅读 1,108评论 0 0
  • 目录 1. 栈和队列1.用两个队列实现栈2.用两个栈实现队列3.实现一个栈,可以用常数级时间找出栈中的最小值4.判...
    MigrationUK阅读 3,143评论 4 20
  • 这天,宋居寒提前一天结束了自己在外地的工作,马不停蹄地飞回京城,想给何故一个惊喜,可是当他兴冲冲地打开门,没有想象...
    天屿流阅读 401评论 0 1
  • 人这一辈子真不容易,一路走过来,经历了风风雨雨,月晴圆缺,也许所有的快乐不一定记得住,但所受的苦难必定会给你成长。...
    王晳若阅读 204评论 0 1
  • 昨天晚上和妈妈聊了会天,睡的有些晚,一觉醒来已经到了七点多,虽然很想偷个懒,但是在心里告诉自己绝对不可以! ...
    宁子沫沫阅读 295评论 0 0

友情链接更多精彩内容