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 = slow.next;
    }
  }
  ListNode current = slow.next;
  ListNode halfFirst = current;
  if(current == null || current.next==null) {
    return head;
  }
  ListNode next = current.next;

  // reverse
  while(next!=null) {
    ListNode temp = current;
    current = next;
    next = next.next;
    current.next = temp;
  }

  //combine
  slow.next = current;
  halfFirst.next = null;
  return head;
}

时间: 2025-01-06 15:12:51

amazon o2 - reverse second half linked list的相关文章

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

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*

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

Reverse Linked List

题目: Reverse a singly linked list. cpp: class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; ListNode *p1 = head; ListNode *p2 = head->next; ListNode *p3 = head->next->next; p1->next = nullpt

206. Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/#/description Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Hint: The key point is to make a copy of the next link of c

Leetcode-206 Reverse Linked List

#206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNo

leetcode:Reverse Linked List

Reverse a singly linked list. 代码如下: the iterative solution:(c++) /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseLis

leetCode 206. Reverse Linked List 反转链表

206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode(int x) : val(x), next(NULL) {}  * };

[LeetCode]77. Reverse Linked List反转链表

Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Subscribe to see which companies asked this question 解法1:一个最简单的办法就是借助栈的后进先出功能,先扫描一遍链表保存每个节点的值,然后再