LeetCode算法练习(反转链表)

本文内容:
1、题目
2、解答
3、递归解答

1、题目

反转一个单链表。

2、解答

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        //反转链表充分体现了临时节点的概念、指针的强大
        ListNode front = null;
        ListNode after = null;
        while(head!=null){
            after = head.next;
            head.next = front;
            front = head;
            head = after;
        }
    }
}

3、递归解答

猜你喜欢

转载自blog.csdn.net/weixin_39471249/article/details/79894034
今日推荐