Reverse Linked List

Reverse a singly linked list.

 

/**
 * 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 pre = null;
        ListNode cur = head;
        ListNode nex = null;
        while (head != null) {
        	nex = head.next;
        	head.next = pre;
        	pre = head;
        	head = nex;
        }
        return pre;
    }
}

 

你可能感兴趣的:(list)