/**
* 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 || head.next == null) return head ;
ListNode tail = head ; // 链表已反转部分的尾结点指针(总是原链表头)
while (tail.next != null) {
ListNode point = tail.next ; // 欲加入结点
tail.next = point.next ; // 接上未反转部分
point.next = head ; // 反转
head = point ; // 改变反转部分头结点指针
}
return head ;
}
}
【剑指Offer】24. 反转链表
猜你喜欢
转载自blog.csdn.net/Activity_Time/article/details/108986892
今日推荐
周排行