My Java solution


  • 7
    P
    public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
    	while (head != null) {
    		ListNode temp = head.next;
    		head.next = prev;
    		prev = head;
    		head = temp;
    	}
    	return prev;
    }
    

    }


  • 0
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode prev = null, curr = head, next = head.next;
        while(curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
    

    Similar to mine :)


  • 0
    S

    My solution was also similar and still leetcode is not accepting it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.