力扣-[中等]92.反轉(zhuǎn)鏈表II

給你單鏈表的頭節(jié)點 head 和兩個整數(shù) left 和 right ,其中 left <= right 。請你反轉(zhuǎn)從位置 left 到位置 right 的鏈表節(jié)點,返回 反轉(zhuǎn)后的鏈表 。

示例 1:

輸入:head = [1,2,3,4,5], left = 2, right = 4
輸出:[1,4,3,2,5]
示例 2:

輸入:head = [5], left = 1, right = 1
輸出:[5]

提示:

鏈表中節(jié)點數(shù)目為 n
1 <= n <= 500
-500 <= Node.val <= 500
1 <= left <= right <= n

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/reverse-linked-list-ii
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

一下子沒思路, 然后想著用最簡單的循環(huán)遍歷唄 .

略思索了一下 ,

廢掉的代碼... 沒寫無 , 寫不下去了, 太沒技術(shù)含量了...... , 渣渣, 沒眼看...

public class LeeCode {

    @Test
    public void reverseBetween(){
        ListNode head = new ListNode();
        head.init();
        int left =2;
        int right = 4;
        ListNode  n = head;
        int x = 0;
        ListNode start = null;
        ListNode end = null;
        Stack<ListNode> stack = new Stack<>();
        for(int i = 1;i<=right;i++){
            if(i+1 == left){
                start = n;
            }
            if(i == left){
                stack.push(n);
            }else if(i + 1 == right){
                stack.push(n);
                stack.push(n.next);
                end = n.next.next;
                break;
            }else if(!stack.empty()){
                stack.push(n);
            }
            n=n.next;
        }
        n = null;
        while (stack.size() > 0 && stack.peek() != null){
            if(n != null){
                n.next = stack.pop();
            }else{
                n = stack.pop();
            }
        }
        start.next = n;
        n.next = end;
        System.out.println(start.val);
        System.out.println(end.val);

        while(start != null){
            System.out.println(start.val);
            if(start.next == null){
                break;
            }
        }
    }
    class ListNode {
        int val;
        ListNode next;
        @Override
        public String toString() {
            return ""+val;
        }

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public void init(){
            ListNode five = new ListNode(5);
            ListNode fore = new ListNode(4,five);
            ListNode three = new ListNode(3,fore);
            ListNode two = new ListNode(2,three);
            this.val = 1;
            this.next = two;
        }
    }
}

然后看了下高級答案:

T-T ... 媽耶.. 高級答案看都看不懂... 捋了半天也沒明白. 引用關(guān)系有點深, 還是循環(huán)更新 . .. 好好看看

class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        for(int i = 1; i < m; i++){
            pre = pre.next;
        }
        head = pre.next;
        for(int i = m; i < n; i++){
            ListNode nex = head.next;
            head.next = nex.next;
            nex.next = pre.next;
            pre.next = nex;
        }
        return dummy.next;
    }
}
  1. 這也是個不錯的答案:
class Solution {
     public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode superior = dummyHead;

        // 1. 遍歷至m的前一個節(jié)點
        for ( int i = 1; i < m; i ++ ) {
            superior = superior.next;
        }

        ListNode prev = null;
        ListNode cur = superior.next;

        // 2. 180°旋轉(zhuǎn)爆炸
        for ( int i = 0; i <= n-m; i ++ ) {
            ListNode next = cur.next;

            cur.next = prev;
            prev = cur;
            cur = next;
        }
        
         // 3. 修改m和n-m位置處的節(jié)點的指向
        superior.next.next = cur;
        superior.next = prev;
        return dummyHead.next;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容