lintcode-easy-Reverse Linked List

Posted 哥布林工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-Reverse Linked List相关的知识,希望对你有一定的参考价值。

Reverse a linked list.

For linked list 1->2->3, the reversed linked list is 3->2->1

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: The new head of reversed linked list.
     */
    public ListNode reverse(ListNode head) {
        // write your code here
        if(head == null || head.next == null)
            return head;
        
        ListNode prev = null;
        ListNode curr = head;
        ListNode next = head.next;
        
        while(next != null){
            curr.next = prev;
            prev = curr;
            curr = next;
            next = next.next;
        }
        curr.next = prev;
        
        return curr;
    }
}

 

以上是关于lintcode-easy-Reverse Linked List的主要内容,如果未能解决你的问题,请参考以下文章

LIN(Local Interconnect Network)总线

Vector - CAPL - 等待并获取LIN数据

LIN通讯

汽车Lin总线特点

G016-OS-LIN-CENT-01 CentOS 7.8.2003 安装

G006-OS-LIN-UBUN-02 Ubuntu Server 22.04 安装