zl程序教程

您现在的位置是:首页 >  其他

当前栏目

反转链表_15

2023-04-18 16:10:22 时间

思路总结:

1.边界值考虑无用质疑 2.原地置换链表,将head作为每次需要处理的结点,pre用于存储之前一个结点 next用于存放下一个结点,也是head的next指向确定好了后要替换的结点

上才艺~咳咳 上代码

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