Jan 23 - Reverse Linked List; Linked List; Pointers;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode cur = head;
        ListNode prev = null;
        int i = 1;
        while(i < m){
            prev = cur;
            cur = cur.next;
            i++;
        }
        ListNode start = cur, end = cur;
        while(i < n){
            ListNode nex = end.next.next;
            end.next.next = start;
            start = end.next;
            end.next = nex;
            cur = nex;
            i++;
        }
        if(prev != null){
            prev.next = start;
            return head;
        }
        return start;
    }
}

Too late night now, no energy to say anything...Just pointer operation.

时间: 2024-08-24 11:51:02

Jan 23 - Reverse Linked List; Linked List; Pointers;的相关文章

Reverse a singly linked list

Reverse a singly linked list. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode*

LeetCode 206 Reverse a singly linked list.

Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 递归的办法: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v

LeetCode[Linked List]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个题目跟Linked List Cycle一样,我也没有能够自己独立找到解决方法,而是借助Discuss学到了一个非常巧妙的解决方法: 首先借助LeetCode[Linked List]: Lin

Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration &amp; Recursion

Iteration: 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseList(ListNode head) { if(head == null) return null; List

amazon o2 - reverse second half linked list

public ListNode reverseBetween(ListNode head, int m, int n) { if(head==null) return head; ListNode slow = head; ListNode fast = head; //find middle while(fast.next!=null) { fast = fast.next; if(fast.next!=null) { fast = fast.next; } else { slow = slo

Reversed Linked List(Reverse a singly linked list)

struct ListNode { int m_nKey; ListNode* next; } ListNode* reverseList(ListNode* pHead) { ListNode* pReversedHead = nullptr; ListNode* pNode = pHead; ListNode* pPrev = nullptr; while(pNode != nullptr){ ListNode* pNext = pNode->next; if(pNext == nullpt

[Linked List]Linked List Cycle,Linked List Cycle II

一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? (M) Linked List Cycle II /** * Definition for singly-

LeetCode[Linked List]: Linked List Cycle

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 这个题目如果没有空间复杂度O(1)的限制,我可以想到的方法就是:遍历整个list,将每个节点的地址存入一个vector,如果发现某个节点的next的地址已经在vector中,那么必然存在cycle. 由于空间复杂度O(1)的限制,我没有想出解决问题的办法,在Discuss上学到

【LeetCode】2.Linked List — Linked List Cycle II 链表环2

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to.