剑指offer-- 反转链表

xiaoxiao2021-02-27  402

题目:

反转链表,输入一个链表,输入该链表的反转

解析:

这道题没有什么思想,就是最基本的链表运算,但是牵扯了很多指针的变换,所以,经常拿出来考,大家熟记就好了

/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode ReverseList(ListNode head) { ListNode pre = null; ListNode next = null; while(head != null){ next = head.next; head.next = pre; pre = head; head = next; } return pre; } }
转载请注明原文地址: https://www.6miu.com/read-5475.html

最新回复(0)