《剑指offer》15.反转链表

题目地址:https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&rp=4&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

解题思路:迭代的思想,pre指向head的前一个结点,q指向head的下一个结点,依次循环,知道head为空。

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null)
            return head;
        ListNode pre=null,q=null;
        while(head!=null){
            q = head.next;
            head.next = pre;
            pre = head;
            head = q;
        }
        return pre;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28900249/article/details/89295600