title: leetcode-206-反转链表(java|python)
date: 2019-09-30 13:48:30
categories:
- leetcode
tags: - leetcode
leetcode-206-反转链表(java|python)
-
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
-
/** * 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 l1 = null; ListNode l2 = head; while(l2!=null){ ListNode l3 = l2.next; l2.next = l1; l1 = l2; l2 = l3; } return l1; } }