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;
             
        }
    }
  • 相关阅读:
    转载:oracle事务的ACID特性
    oracle对象之存储函数
    转载:散列冲突的解决策略
    [Linux]Redhat7配置本地镜像源
    [Linux]Redhat7配置CentOS7 YUM源
    java120经典面试题
    Dijkstra算法
    树的重构
    第三章垃圾收集器与内存分配策略
    内存区域与内存溢出异常
  • 原文地址:https://www.cnblogs.com/wangcl-8645/p/11239962.html
Copyright © 2011-2022 走看看