代码如下: class Solution { public: void reorderList(ListNode* head) { vector<ListNode*> vec; 这种方法比操作数组容易一些,不用双指针模拟一前一后了 class Solution { public: void reorderList(ListNode* head) { deque pre = cur; cur = temp; } return pre; } public: void reorderList = nullptr) { cur->next = cur1; } } }; 其他语言版本 Java // 方法三 public class ReorderList { public void reorderList(ListNode head) { ListNode fast = head, slow = head; //
init__(self, x): # self.val = x # self.next = None class Solution(object): def reorderList ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorderList
. */ var reorderList = function(head) { if (head == null) return // 翻转链表 function reverseList( = beforeList afterList = after } return head } 存储链表节点 使用数组存储节点 声明两个指针分别从数组首尾开始构建 next var reorderList
三、代码 import utils.ListNode; import java.util.Stack; public class Solution { public void reorderList node2; node2.next=node3; node3.next=node4; node4.next=null; solution.reorderList
init(_ val: Int) { self.val = val self.next = nil }}func reorderList(_ head: ListNode next } print()}// 示例 1let head1 = createLinkedList([1, 2, 3, 4])reorderList(head1)printLinkedList (head1) // 输出: 1 4 2 3// 示例 2let head2 = createLinkedList([1, 2, 3, 4, 5])reorderList(head2)printLinkedList
val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public void reorderList
val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public void reorderList val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public void reorderList
class Solution { public void reorderList(ListNode head) { /** 使用双端队列 node.next
ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorderList
代码 (1) 方法一:从中间点prev处开始逆序 public void reorderList(ListNode head) { if (head == null || head.next cur = cur.next; cur2 = cur2.next; } } (2) 方法二:从中间点next处开始逆序 public void reorderList
ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorderList
* ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void reorderList
ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorderList
ListNode { * int val; * struct ListNode *next; * }; */ typedef struct ListNode ListNode; void reorderList /注意先后顺序 cur->next=phead; phead=cur; cur=tmp; } return phead; } void reorderList
翻转后一段 拼接 代码 class Solution: def reorderList(self, head): """ :type head: ListNode
ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void reorderList
这道题是道综合题,把三个知识点串起来,非常适合复习链表处理的三个技巧 【思路】:观察发现可以把链表后一半进行反转,然后当成两个链表的合并任务即可 class Solution { public: void reorderList
2、代码实现 代码参考: class Solution { public void reorderList(ListNode head) { if (head == null)
fast.next.next; slow = slow.next; } return slow; } public void reorderList
* ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void reorderList