当前位置:网站首页>Reverse linked list drawing demonstration

Reverse linked list drawing demonstration

2022-07-24 00:23:00 T.Y.Bao

subject

Here's the head node of the list head , Please reverse the list , And return the inverted linked list . Please add a picture description

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
    
    public ListNode reverseList(ListNode head) {
    

    }
}

Answer key

class Solution {
    
    public ListNode reverseList(ListNode head) {
    
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
    
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}

 author :LeetCode-Solution
 link :https://leetcode.cn/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/
 source : Power button (LeetCode)
 The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .

Drawing demonstration

 Please add a picture description

原网站

版权声明
本文为[T.Y.Bao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231157523210.html