zoukankan      html  css  js  c++  java
  • leetcode——206. 反转链表

    头依次拔下来,按顺序插进另一个链表:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseList(self, head: ListNode) -> ListNode:
            new_head=None
            while head:
                tmp=head.next
                head.next=new_head
                new_head=head
                head=tmp
            return new_head
    执行用时 :44 ms, 在所有 python3 提交中击败了93.17%的用户
    内存消耗 :14.9 MB, 在所有 python3 提交中击败了19.62%的用户
     
     
     
     
    ——2019.10.24
     
    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode dump = new ListNode(-1);
            ListNode p = dump.next;
            ListNode q;
            while(head != null){
                q = new ListNode(head.val);
                dump.next = q;
                q.next = p;
                p = q;
                head = head.next;
            }
            return dump.next;
        }
    }

     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    Java 中的POJO和JavaBean 的区别
    设计模式的六大原则
    AOP
    Jetbrains 全家桶
    centos7 如何关闭防护墙
    Java 面试题常见范围
    putty readme
    单机环境
    flask-caching缓存
    1.restful 规范与APIView
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11734802.html
Copyright © 2011-2022 走看看