leetcode 题解记录

114 阅读1分钟

1、 反转单链表 使用双指针
leetcode-cn.com/problems/fa…

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode cur=null,pre= head;
        while(pre!=null){
            ListNode t = pre.next;
            pre.next = cur;
            cur = pre;
            pre=t;
        }
        return cur;
    }
}