zoukankan      html  css  js  c++  java
  • Reverse Linked List

    反转的方法
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public int val;
     *     public ListNode next;
     *     public ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode ReverseList(ListNode head) {
            if(head==null||head.next==null)
                return head;
            ListNode node=new ListNode(0);
            node.next=head;
            while(head.next!=null){
                ListNode temp=head.next;
                head.next=head.next.next;
                temp.next=node.next;
                node.next=temp;
            }
            
            return node.next;
             
        }
    }
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public int val;
     *     public ListNode next;
     *     public ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode ReverseList(ListNode head) {
            if(head==null)
                return head;
            ListNode pre=head;
            ListNode cur=head.next;
            while(cur!=null){
                pre.next=cur.next;
                cur.next=head;
                head=cur;
                cur=pre.next;
            }
            return head;
             
        }
    }
    递归调用 
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public int val;
     *     public ListNode next;
     *     public ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode ReverseList(ListNode head) {
           if(head==null||head.next==null)
               return head;
            ListNode newHead=ReverseList(head.next);
            head.next.next=head;
            head.next=null;
            return newHead;
             
        }
    }
  • 相关阅读:
    pymysql
    flask WTForms
    线程安全问题
    flask学习2
    @functools.wraps(func)
    Solidity开发神器Remix
    Web3j实现智能合约
    基于Ubuntu Docker环境下进行以太坊实践
    以太坊RLP机制分析
    以太坊网络服务分析
  • 原文地址:https://www.cnblogs.com/wangcl-8645/p/11239962.html
Copyright © 2011-2022 走看看