leetcode 143. Reorder List 双指针

柔情只为你懂 2022-06-08 06:06 263阅读 0赞

Given a singly linked list L: L0?L1?…?Ln-1?Ln,
reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…

You must do this in-place without altering the nodes’ values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

最好的方法是使用双指针来得到中间结点,然后使用栈和队列来做。

要是直接遍历然后使用反序列表来做,也可以,不过非常烦。

代码如下:

  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Stack;
  4. /*class ListNode
  5. {
  6. int val;
  7. ListNode next;
  8. ListNode(int x) { val = x; }
  9. }*/
  10. public class Solution
  11. {
  12. public void reorderList(ListNode head)
  13. {
  14. if(head==null || head.next==null)
  15. return ;
  16. //使用快慢指针把链表分为两个
  17. ListNode slow=head;
  18. ListNode fast=head;
  19. while(fast.next!=null && fast.next.next!=null)
  20. {
  21. fast=fast.next.next;
  22. slow=slow.next;
  23. }
  24. ListNode head1=head , head2=slow.next;
  25. slow.next=null;
  26. Queue<ListNode> myQueue=new LinkedList<>();
  27. ListNode cur=head1;
  28. while(cur!=null)
  29. {
  30. myQueue.add(cur);
  31. cur=cur.next;
  32. }
  33. Stack<ListNode> myStack=new Stack<>();
  34. cur=head2;
  35. while(cur!=null)
  36. {
  37. myStack.add(cur);
  38. cur=cur.next;
  39. }
  40. ListNode fin=new ListNode(-1);
  41. cur=fin;
  42. while(myQueue.isEmpty()==false && myStack.isEmpty()==false)
  43. {
  44. cur.next=myQueue.poll();
  45. cur=cur.next;
  46. cur.next=myStack.pop();
  47. cur=cur.next;
  48. }
  49. while(myQueue.isEmpty()==false)
  50. {
  51. cur.next=myQueue.poll();
  52. cur=cur.next;
  53. }
  54. while(myStack.isEmpty()==false)
  55. {
  56. cur.next=myStack.pop();
  57. cur=cur.next;
  58. }
  59. //这是最后一步,一定要设置
  60. cur.next=null;
  61. head=fin.next;
  62. }
  63. public static void main(String[] args) {
  64. ListNode one=new ListNode(1);
  65. one.next=new ListNode(2);
  66. Solution solution=new Solution();
  67. solution.reorderList(one);
  68. }
  69. }

下面是C++的做法,就是使用双指针来解决这个问题

代码如下:

  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. using namespace std;
  5. /*
  6. struct ListNode
  7. {
  8. int val;
  9. ListNode *next;
  10. ListNode(int x) : val(x), next(NULL) {}
  11. };
  12. */
  13. class Solution
  14. {
  15. public:
  16. void reorderList(ListNode* head)
  17. {
  18. if (head == NULL || head->next == NULL)
  19. return;
  20. ListNode* fast = head;
  21. ListNode* slow = head;
  22. while (fast->next != NULL && fast->next->next != NULL)
  23. {
  24. fast = fast->next->next;
  25. slow = slow->next;
  26. }
  27. ListNode* h1 = head;
  28. ListNode* h2 = slow->next;
  29. slow->next = NULL;
  30. queue<ListNode*> que;
  31. ListNode* i = h1;
  32. while (i != NULL)
  33. {
  34. que.push(i);
  35. i = i->next;
  36. }
  37. stack<ListNode*> skt;
  38. i = h2;
  39. while (i != NULL)
  40. {
  41. skt.push(i);
  42. i = i->next;
  43. }
  44. ListNode* fin = new ListNode(-1);
  45. i = fin;
  46. while (!que.empty() && !skt.empty())
  47. {
  48. i->next = que.front();
  49. que.pop();
  50. i = i->next;
  51. i->next = skt.top();
  52. skt.pop();
  53. i = i->next;
  54. }
  55. while (!que.empty())
  56. {
  57. i->next = que.front();
  58. que.pop();
  59. i = i->next;
  60. }
  61. while (!skt.empty())
  62. {
  63. i->next = skt.top();
  64. skt.pop();
  65. i = i->next;
  66. }
  67. i->next = NULL;
  68. head = fin->next;
  69. }
  70. };

发表评论

表情:
评论列表 (有 0 条评论,263人围观)

还没有评论,来说两句吧...

相关阅读