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

    Reverse a singly linked list.

    Hint:

    A linked list can be reversed either iteratively or recursively. Could you implement both?


    Java Code:
    Iteratively: 
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode reverseList(ListNode head) {
            if(head == null || head.next == null) {
                return head;
            }
            
            ListNode p1 = head;
            ListNode p2 = head.next;
            head.next = null;
            while(p1 != null && p2 != null) {
                ListNode p3 = p2.next;
                p2.next = p1;
                p1 = p2;
                if(p3!= null) {
                    p2 = p3;
                }else {
                    break;
                }
            }
            return p2;
        }
    }

    Recursively:

    版本一:

     public ListNode reverseList(ListNode head) {
           if(head==null || head.next == null)
            return head;
     
            //get second node    
            ListNode second = head.next;
            //set first's next to be null
            head.next = null;
         
            ListNode rest = reverseList(second);
            second.next = head;
         
            return rest; 
        }

    版本二:

     public ListNode reverseList(ListNode head) {
            return reverseListInt(head, null);
        }
    
        public ListNode reverseListInt(ListNode head, ListNode newHead) {
            if(head == null)
                return newHead;
            ListNode next = head.next;
            head.next = newHead;
            return reverseListInt(next, head);
        }

    20160601:

    iteration:

    public class Solution {
        public ListNode reverseList(ListNode head) {
         //iteration
            //base case
            if(head == null || head.next == null) {
                return head;
            }
            
            ListNode pre = null;
            ListNode cur = head;
            ListNode next = null;
            while(cur != null) {
                next = cur.next;
                cur.next = pre;
                pre = cur;
                cur = next;
            }
            return pre;
            }
    }

    recursion:

    public class Solution {
        public ListNode reverseList(ListNode head) {
          //recursion
            //base case
            if(head == null || head.next == null) {
                return head;
            }
            ListNode nextNode = head.next;
            ListNode newHead = reverseList(nextNode);
            nextNode.next = head;
            head.next = null;
            return newHead;
        }
    }

    Reference:

    1. http://www.programcreek.com/2014/05/leetcode-reverse-linked-list-java/

    2. https://leetcode.com/discuss/34474/in-place-iterative-and-recursive-java-solution

     
  • 相关阅读:
    Codeforces Round #706 (Div. 2)
    Caddi Programming Contest 2021(AtCoder Beginner Contest 193)
    [ARC116] Deque Game
    Codeforces Round #721 (Div. 2)
    Codeforces Round #618 (Div. 1)
    Educational Codeforces Round 109 (Rated for Div. 2)
    [ABC201F] Insertion Sort
    AtCoder Regular Contest 119
    Codeforces Global Round 13
    Codeforces Round #673 (Div. 1)
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4799763.html
Copyright © 2011-2022 走看看