LeetCode Algorithm Exercise (Reverse Linked List)

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

1、题目

Reverse a singly linked list.

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、递归解答

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559793&siteId=291194637