Leetcode 92.反转链表
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 92.反转链表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
92.反轉(zhuǎn)鏈表
反轉(zhuǎn)從位置 m 到 n 的鏈表。請使用一趟掃描完成反轉(zhuǎn)。
說明:
1 ≤?m?≤?n?≤ 鏈表長度。
示例:
輸入: 1->2->3->4->5->NULL, m = 2, n = 4
輸出: 1->4->3->2->5->NULL
?
詳解見圖:
?
1 public class Solution { 2 public class ListNode { 3 int val; 4 ListNode next; 5 6 ListNode(int x) { 7 val = x; 8 } 9 } 10 11 public ListNode reverseBetween(ListNode head, int m, int n) { 12 if (head == null) { 13 return null; 14 } 15 ListNode dummy = new ListNode(0); 16 dummy.next = head; 17 ListNode prev = dummy; 18 for (int i = 0; i < m - 1; i++) { 19 prev = prev.next; 20 } 21 ListNode cur = prev.next; 22 ListNode post = cur.next; 23 for(int i=0;i<n-m;i++){ 24 cur.next=post.next; 25 post.next=prev.next; 26 prev.next=post; 27 post=cur.next; 28 } 29 return dummy.next; 30 } 31 }?
轉(zhuǎn)載于:https://www.cnblogs.com/kexinxin/p/10163072.html
總結(jié)
以上是生活随笔為你收集整理的Leetcode 92.反转链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快速创建springBoot
- 下一篇: CodeForces1082G Pety