Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linked List Problem - K reverse linked list

Linked List Problem - K reverse linked list

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomdata-structurestutorialquestion
7 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 14023099
    wrote on last edited by
    #1

    Hi, I need help in solving the Linked List K reverse linked list problem.

    Given a singly linked list and an integer K, reverses the nodes of the

    list K at a time and returns modified linked list.

    NOTE : The length of the list is divisible by K
    Example :

    Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,

    You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5

    Try to solve the problem using constant extra space.

    Can anyone point me in the right direction? Thanks.

    CPalliniC D S D 4 Replies Last reply
    0
    • U User 14023099

      Hi, I need help in solving the Linked List K reverse linked list problem.

      Given a singly linked list and an integer K, reverses the nodes of the

      list K at a time and returns modified linked list.

      NOTE : The length of the list is divisible by K
      Example :

      Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,

      You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5

      Try to solve the problem using constant extra space.

      Can anyone point me in the right direction? Thanks.

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      The right direction is forward. :rolleyes: Using an array of K pointers would make the trick.

      In testa che avete, signor di Ceprano?

      S 1 Reply Last reply
      0
      • U User 14023099

        Hi, I need help in solving the Linked List K reverse linked list problem.

        Given a singly linked list and an integer K, reverses the nodes of the

        list K at a time and returns modified linked list.

        NOTE : The length of the list is divisible by K
        Example :

        Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,

        You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5

        Try to solve the problem using constant extra space.

        Can anyone point me in the right direction? Thanks.

        D Offline
        D Offline
        Daniel Pfeffer
        wrote on last edited by
        #3

        Try to break the problem into sub-problems. For example: 1. Given a singly-linked list of length k, reverse the order of the elements 2. Given a singly-linked list of length 2*k, reverse the order of the first k elements, without touching the last k elements. Return a pointer to the _k_th element (counting from zero). 3. Given a singly-linked list of length 2*k, reverse the order of the last k elements. 4. Given a singly-linked list of length 3*k, reverse the order of the middle k elements. return a pointer to the _2*k_th element (counting from zero). 5. Put everything together to get the solution to your problem.

        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

        1 Reply Last reply
        0
        • CPalliniC CPallini

          The right direction is forward. :rolleyes: Using an array of K pointers would make the trick.

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          CPallini wrote:

          Using an array of K pointers

          Wouldn't that break the condition to only use a constant amount of extra space (assuming this refers to memory usage)?

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          CPalliniC 1 Reply Last reply
          0
          • U User 14023099

            Hi, I need help in solving the Linked List K reverse linked list problem.

            Given a singly linked list and an integer K, reverses the nodes of the

            list K at a time and returns modified linked list.

            NOTE : The length of the list is divisible by K
            Example :

            Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,

            You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5

            Try to solve the problem using constant extra space.

            Can anyone point me in the right direction? Thanks.

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            Break the problem into simpler tasks and implement functions for these: 1. write a function that reverses a full list. Make sure to adhere to the constraint to only use a constant amount of space (memory). 2. write a function to find the Nth element within a list from a given starting position (not actually needed, but will simplify the final program) 3. write a function to split off part of a given list before the Nth position 4. write code to retrieve sub-lists of length K using 2. and 3. and code to insert a (modified) sublist back at the original position. 5. use 1. and 4. in an iteration to process the entire list All of these steps can easily be done with the help of at most two or three temporary references (pointers). The only tricky one is the reverse function.

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            1 Reply Last reply
            0
            • U User 14023099

              Hi, I need help in solving the Linked List K reverse linked list problem.

              Given a singly linked list and an integer K, reverses the nodes of the

              list K at a time and returns modified linked list.

              NOTE : The length of the list is divisible by K
              Example :

              Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,

              You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5

              Try to solve the problem using constant extra space.

              Can anyone point me in the right direction? Thanks.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Member 14055543 wrote:

              Can anyone point me in the right direction?

              What code do you have so far? What help have you sought from your instructor? What does the problem/solution look like on paper (don't bother with code until you can complete this step)?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              1 Reply Last reply
              0
              • S Stefan_Lang

                CPallini wrote:

                Using an array of K pointers

                Wouldn't that break the condition to only use a constant amount of extra space (assuming this refers to memory usage)?

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                CPalliniC Online
                CPalliniC Online
                CPallini
                wrote on last edited by
                #7

                Yes, you are right. I was focused on the space complexity related to the list length. I see my assumption the 'the algo is O(1)' only holds if K is a constant.

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups